What is a quick way to get the file name?

myVar = "D:\\mainfolder\\subf1\\subf2\\subf3\\file.txt";

How can I get file.txtcomfortable in .NET 2.0 with C #? What I know is split using \\and trying to get the last element.

Thank.

+5
source share
2 answers
string fileNameOnly = Path.GetFileName("D:\\mainfolder\\subf1\\subf2\\subf3\\file.txt");

must return file.txt

Binding Path Class Documentation on MSDN. It has many other convenient methods.

+11
source
System.IO.Path.GetFileName(myVar);
+7
source

All Articles