If path = "\ProgramFiles\MobileApp\es-gl\a.dll". I want to get "\ProgramFiles\MobileApp\es-gl" alone. Just want to know the parent directory of the file a.dll. Is there Any inbuilt method in c#? I am using .net Compact Framework
Asked
Active
Viewed 7,838 times
6 Answers
3
I'm not sure but I think the FileInfo
and DirectoryInfo
classes are supported on the Compact Framework.
Try this:
FileInfo myFile = new FileInfo("\ProgramFiles\MobileApp\es-gl\a.dll");
string parentDirectory = myFile.Directory.Name;
According to the MSDN documentation you could also do this:
FileInfo myFile = new FileInfo("\ProgramFiles\MobileApp\es-gl\a.dll");
string parentDirectory = myFile.DirectoryName;
Check out these MSDN links for more info:
http://msdn.microsoft.com/en-us/library/system.io.fileinfo_members(v=vs.71)
http://msdn.microsoft.com/en-us/library/system.io.fileinfo.directory(v=vs.71)

weston
- 54,145
- 21
- 145
- 203

Gerald Versluis
- 30,492
- 6
- 73
- 100
3
I also needed such a function to find the parent directory of a folder seamlessly. So I created one myself:
public static string ExtractFolderFromPath(string fileName, string pathSeparator, bool includeSeparatorAtEnd)
{
int pos = fileName.LastIndexOf(pathSeparator);
return fileName.Substring(0,(includeSeparatorAtEnd ? pos+1 : pos));
}
Just send pathSeparator ("\" for windows and "/" for unix-like paths). set last parameter true if you want separator included at the end. for ex: C:\foo\

Prahlad Yeri
- 3,567
- 4
- 25
- 55
-
1At least use `System.IO.Path.DirectorySeparatorChar` – weston Aug 08 '12 at 10:20
-
@weston - In my ftp program, this won't work. I have to handle both local directory splitter ("\") and the remote ftp folder splitter ("/"). Hence the splitter argument. – Prahlad Yeri Aug 08 '12 at 10:41
-
At least use "\\" or @"\" then :-) – Jason Williams Aug 08 '12 at 11:04
-
Then I think you are mixing file paths with URIs and treating them both as strings. Consider using the `Uri` class for the ftp address, and adding this method as an extension method to it. And use plain `System.IO.Path.GetDirectoryName(path)` for file path tasks. – weston Aug 08 '12 at 11:13
-
1At the time I had written my ftp program, I was not aware of the benifits of System.Uri class. Now, I feel that Uri is indeed the way to go since it takes away the burden of parsing. – Prahlad Yeri Aug 08 '12 at 11:39
-
@JasonWilliams - Indeed, when passing arguments to this function, I use the @"\", and not "\". – Prahlad Yeri Aug 08 '12 at 11:40
2
There is a Parent directory on FileInfo(System.IO namespace). Example code :
var file = new FileInfo(@"\ProgramFiles\MobileApp\es-gl\a.dll");
var parent = file.Directory.Parent;

Jonas W
- 3,200
- 1
- 31
- 44
0
var directory = Path.GetDirectoryName(@"c:\some\path\to\a\file.txt");
// returns "c:\some\path\to\a"

Jason Williams
- 56,972
- 11
- 108
- 137

mdm
- 12,480
- 5
- 34
- 53
-
This is not working for Compact Framework. Do I have any solution for Compact Framework? – Badhri Ravikumar Aug 08 '12 at 08:54
-
@weston I think you cannot use Path.GetDirectoryName in Compact Framework. – Badhri Ravikumar Aug 08 '12 at 11:29
-
@BadhriRavikumar It is supported in .NET Compact Framework versions 3.5, 2.0, and 1.0 - check the MSDN link in my answer. – mdm Aug 08 '12 at 12:01
0
You can just use the methods of the string class.
string path = @"\ProgramFiles\MobileApp\es-gl\a.dll";
string newPath = path.Substring(0, path.LastIndexOf('\\'));

Icy Creature
- 1,875
- 2
- 28
- 53