Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse directory name from a full filepath in C#

Tags:

c#

file

If I have a string variable that has:

"C:\temp\temp2\foo\bar.txt" 

and I want to get

"foo"

what is the best way to do this?

like image 395
leora Avatar asked Mar 19 '09 00:03

leora


People also ask

How do I get the full path of a filename?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How can I get a directory name?

Get the directory name with dirname() PHP's dirname() function returns just the directory part of the full path.

Does full path include filename?

Paths include the root, the filename, or both. That is, paths can be formed by adding either the root, filename, or both, to a directory.

Which property is used to get the full path of the file?

The FullName property returns just the full path of a file including the file name.


1 Answers

Use:

new FileInfo(@"C:\temp\temp2\foo\bar.txt").Directory.Name 
like image 141
Jon Skeet Avatar answered Sep 20 '22 13:09

Jon Skeet