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
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
Use Path.GetFileName
:
string result = Path.GetFileName(@"d:\MyDir\Tmp\MyFile.txt");
Console.WriteLine(result); // outputs MyFile.txt
string path = @"d:\MyDir\Tmp\MyFile.txt";
System.IO.Path.GetFileName(path);
string path = @"d:\MyDir\Tmp\MyFile.txt";
FileInfo fileInfo = new FileInfo(path);
Console.WriteLine(fileInfo.Name);
// or
Console.WriteLine(System.IO.Path.GetFileName(path));
Use this:
FileInfo fileInfo = new FileInfo(path);
fileInfo.Name
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")