C# – How to get a part from the full path in C#

cdirectorystring

I have a full path as given below.

C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd

How can the DTDs "part" to be fetched from this whole part?

Desired output:

C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug‌​\DannyGoXuk.DTDs

Can I use String's methods for this?
If yes, then how to fetch it?

Best Answer

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


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

If that's the case, given that entire new path, you want something like this to 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 properties window i choose Build Type as Embedded resource.

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