tags:

views:

67

answers:

5

Possible Duplicates:
How to get a filename from a path?
How to extract file name from file path name?

hi

if i have this path: d:\MyDir\Tmp\MyFile.txt

i need to get the name of the file (only the MyFile.txt)

how to do it ?

thank's in advance

+5  A: 

Use Path.GetFileName:

string result = Path.GetFileName(@"d:\MyDir\Tmp\MyFile.txt");
Console.WriteLine(result); // outputs MyFile.txt
Oded
+4  A: 
string path = @"d:\MyDir\Tmp\MyFile.txt";
System.IO.Path.GetFileName(path);
Mark Avenius
+1  A: 
string path = @"d:\MyDir\Tmp\MyFile.txt";
FileInfo fileInfo = new FileInfo(path);
Console.WriteLine(fileInfo.Name);

// or
Console.WriteLine(System.IO.Path.GetFileName(path));
rchern
+1  A: 

Use this:

    FileInfo fileInfo = new FileInfo(path);
    fileInfo.Name
Andrew Bezzub
A: 

You could also do a split on the string using "\" as a separator, then simply get the last item of the array. Could be a bit more useful if you don't use full file paths, especially if your working with partial paths on a different server (such as where a file might be stored "\images\blah.jpg")

Anthony Greco
Don't do this. `Path.GetFileName` does handle partial paths in a reasonable manner, and this is really not something you should be implementing yourself, despite its simplicity.
Brian