且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获得路径的姓氏

更新时间:2023-01-04 12:55:14

您只需要两个链接:
http://msdn.microsoft.com/en-us/library /system.io.path.getfilename.aspx [ ^ ],
Microsoft Q209354 .

祝你好运,
—SA
You need just two links:
http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx[^],
Microsoft Q209354.

Good luck,
—SA


有多种方法:可以使用字符串方法或FileInfo方法,具体取决于"2222"是什么:
There are a number of ways: You could use string methods, or FileInfo methods, depending on what "2222" is:
string path = @"d:\myfolder\myTest\testfolder\111111\2222";
FileInfo fi = new FileInfo(path);
Console.WriteLine(fi.Name);
string last = path.Substring(path.LastIndexOf('\\') + 1);
Console.WriteLine(last);

实际上,这两种方法都产生相同的结果,而且它们都不要求甚至存在文件夹结构/磁盘的文件.但是,FileInfo方法将要求该路径为有效的文件规范-例如,如果给它指定:D \ myfolder \ myTest \ testfolder \ 111111 \ 2222",它将引发异常.

In fact, both methods produce the same result, and neither of them require the file of even the folder structure / disk to exist. However, the FileInfo method will require the path to be a valid file specification - it will throw an exception if you give it ":D\myfolder\myTest\testfolder\111111\2222" for example.



使用 String.Substring(),您可以执行很容易,

Using String.Substring() you can do it easily,
string path = @"d:\myfolder\myTest\testfolder\111111\2222";
          path = path.Substring(path.LastIndexOf(@"\") + 1);