Reputation: 69
If string path = "\\ProgFiles\\sampleDir\\annet.dll"
I want to take "\\ProgFiles\\sampleDir"
alone from the path in a seperate string variable using c#. Do I have any inbuilt option for this? I am using visual studio 2008 and .net compact framework.
Upvotes: 0
Views: 154
Reputation: 4253
You can use the FileInfo class to do that, just try something like this
FileInfo fi = new FileInfo("Your path here");
string dirName = fi.DirectoryName;
Upvotes: 0
Reputation: 42260
You could try:
String path = "C:\\ProgFiles\\SampleDir\\annet.dll";
String newPath = path.Substring(0, path.LastIndexOf("\\"));
The syntax may be a little out (i have not tested it), but definitely look up .Substring and .LastIndexOf methods on strings!
Upvotes: -2
Reputation: 1075
string directory = Path.GetDirectoryName(path);
Be aware that there are some nuances with this method (like returning null for a root directory): check out the MSDN.
Upvotes: 3
Reputation: 150
Take a look at the System.IO.Path class. It contains a Method "GetDirectoryName". That's what you should need.
Upvotes: 1