Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the fast way to get the file name?

Tags:

c#

.net

c#-2.0

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

How can I get file.txt conveniently in .NET 2.0 with C#? What I know is split with \\ and try to get the last member.

Thanks.

like image 672
Nano HE Avatar asked Sep 16 '11 05:09

Nano HE


2 Answers

string fileNameOnly = Path.GetFileName("D:\\mainfolder\\subf1\\subf2\\subf3\\file.txt");

should return file.txt

Linking Path's class documentation on MSDN. Has many other convenient methods.

like image 93
Icarus Avatar answered Sep 19 '22 19:09

Icarus


System.IO.Path.GetFileName(myVar);
like image 30
Aviad P. Avatar answered Sep 19 '22 19:09

Aviad P.