Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 227
crazy_itgal Avatar asked May 26 '09 20:05

crazy_itgal


People also ask

How to extract file name from path?

To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null. Syntax: public static string GetFileName (string path);

Which Method retrieves the full path of the current directory?

If you specify a file name only in path , GetFullPath returns the fully qualified path of the current directory.


1 Answers

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".

like image 130
Joel Coehoorn Avatar answered Oct 06 '22 03:10

Joel Coehoorn