如何获得路径的姓氏 [英] how to get a last name of the path

查看:81
本文介绍了如何获得路径的姓氏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我有一条路

Hello,
I have a path

d:\myfolder\myTest\testfolder\111111\2222


我需要获取路径的最后一部分,即2222

请帮助我.


I need to get the last part of the path i.e 2222

Please help me.

推荐答案

您只需要两个链接:
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);


这篇关于如何获得路径的姓氏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆