我正在使用C#,并希望从string中只填写具有全名的数据名称
例如我在string中有这个名字: D:\ Users \ admin \ Documents \ file.txt ,并且只想在string中有file.txt
怎么做?
您可以使用Path类的GetFileName方法:
String path = "D:\Users\admin\Documents\file.txt"; string name = System.IO.Path.GetFileName(path);
Path.GetFileName
将返回你所需要的。
string fileName = @"D:\Users\admin\Documents\file.txt"; string result; result = Path.GetFileName(fileName);
result
将是“file.txt”。
如果你想使用文件扩展名
System.IO.Path.GetFileName(path);
如果你想不使用文件扩展名
System.IO.Path.GetFileNameWithoutExtension(path);
使用Path.GetFileName
方法 :
string fileName = Path.GetFileName(path);
正如aleroot所说,你想使用System.IO.Path
类。 下面你将如何使用它:
string strFullFilePath = "D:\Users\admin\Documents\file.txt"; string strFileNameOnly = System.IO.Path.GetFileName(strFullFilePath);
我希望这有帮助。