2012-05-28 29 views

回答

5

可以使用Path类的方法GetFileName

String path = "D:\Users\admin\Documents\file.txt"; 
string name = System.IO.Path.GetFileName(path); 
2

Path.GetFileName将返回你所需要的。

string fileName = @"D:\Users\admin\Documents\file.txt"; 
string result; 

result = Path.GetFileName(fileName); 

result将是“file.txt”。

2

如果你想与文件扩展名使用

System.IO.Path.GetFileName(path); 

如果不想文件扩展名使用

System.IO.Path.GetFileNameWithoutExtension(path); 
2

由于aleroot指出的那样,你想使用System.IO.Path类。下面你将如何使用它:

string strFullFilePath = "D:\Users\admin\Documents\file.txt"; 
string strFileNameOnly = System.IO.Path.GetFileName(strFullFilePath); 

我希望这可以帮助。

相关问题