Badhri Ravikumar
Badhri Ravikumar

Reputation: 69

Finding the root path from the given path

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

Answers (4)

Vamsi
Vamsi

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

Matthew Layton
Matthew Layton

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

SomeWritesReserved
SomeWritesReserved

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

mw_21
mw_21

Reputation: 150

Take a look at the System.IO.Path class. It contains a Method "GetDirectoryName". That's what you should need.

Upvotes: 1

Related Questions