[ACCEPTED]-How to get a part from the full path in C#?-directory
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
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
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".
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
You can also use Directory to get the directory from 1 the full file path:
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;
System.IO.Path.GetFileName( System.IO.Path.GetDirectoryName( fullPath ) )
That will return just the name of the folder 2 containing the file.
this will return
I'm 1 inferring that that's what you want.
string dirName = new DirectoryInfo(fullPath).name;
You can use:
You can use Path ...
Don't use string manipulation directly. Instead 1 use
GetDirectoryName of the Path class:
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.
Path.GetDirectory on the path you have specified returns:
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