[ACCEPTED]-How to get a part from the full path in C#?-directory

Accepted answer
Score: 76

Use System.IO.Path.GetDirectoryName() for the entire path, or new DirectoryInfo(path).Parent.Name for just the 13 name of that one folder.


There is no directory named "DTDs" in the path you posted. IT looks like 12 there's a file named "DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd", but the periods (.) in 11 that path are not valid directory separator 10 characters. Did you mean "DannyGoXuk\DTDs\xhtml-math-svg-flat.dtd"?

If that's the 9 case, given that entire new path, you want something like this to 8 return a list of files in the DTDs folder:

string path = @"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk\DTDs\xhtml-math-svg-flat.dtd";
string[] files = new DirectoryInfo(path).Parent.GetFiles();

in 7 properties window i choose Build Type as 6 Embedded resource.

And now we finally get 5 to it. When you choose "Embedded Resource", the 4 item is bundled into your executable program 3 file. There is no direct path anymore. Instead, set your Build Type to 2 "Content" and set "Copy to Output Directory" to 1 "Copy Always" or "Copy if Newer".

Score: 12

Calling

System.IO.Path.GetFileName

with the full directory path returns 5 the last part of the path which is a directory 4 name. GetDirectoryName returns the whole path of parent 3 directory which is unwanted.

If you have 2 a file name and you just want the name of 1 the parent directory:

var directoryFullPath = Path.GetDirectoryName(@"C:\DTDs\mydtd.dtd");  // C:\DTDs
var directoryName = Path.GetFileName(directoryFullPath);  // DTDs
Score: 9

You can also use Directory to get the directory from 1 the full file path:

Directory.GetParent(path).FullName
Score: 7

Edit: Please read the OP’s question and 9 all of her comments carefully before downvotiong 8 this. The OP’s title question isn’t EXACTLY 7 what she wanted. My answer gave her what 6 she needed to solve her problem. Which is 5 why she voted it the answer. Yes, Joel’s 4 answer is correct if specifically answering 3 the title question. But after reading her 2 comments, you’ll see that not exactly what 1 she was looking for. Thanks.

Use this ...

string strFullPath = @"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd";
string strDirName; 
int intLocation, intLength;

intLength = strFullPath.Length;
intLocation = strFullPath.IndexOf("DTDs");

strDirName = strFullPath.Substring(0, intLocation); 

textBox2.Text = strDirName;
Score: 5
System.IO.Path.GetFileName( System.IO.Path.GetDirectoryName( fullPath ) )

That will return just the name of the folder 2 containing the file.

For

C:\windows\system32\user32.dll

this will return

system32

I'm 1 inferring that that's what you want.

Score: 5

Use:

string dirName = new DirectoryInfo(fullPath).name;

0

Score: 2

You can use:

System.IO.Path.GetDirectoryName(path);

0

Score: 2

You can use Path ...

Path.GetDirectoryName(myStr);

0

Score: 2

Don't use string manipulation directly. Instead 1 use GetDirectoryName of the Path class:

System.IO.Path.GetDirectoryName(myPath);
Score: 2

Use the FileInfo object...

FileInfo info = new FileInfo(@"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd");
string directoryName = info.Directory.FullName;

The file doesn't 1 even have to really exist.

Score: 1

Path.GetDirectory on the path you have specified returns:

"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug"

Try 2 it yourself:

var path = Path.GetDirectoryName(@"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd");

Your question is a little strange 1 though- there is no directory called DTDs.

More Related questions