开发者

How can I find the file name in a path in C#?

I have this pat开发者_运维问答h:

C:\wamp\www\tm\23786.txt

I want to get the "23786.txt"

I know we could use explode in php, but how would I do it in C#?


The equivalent to PHP's explode would be string.Split, but you don't need to and shouldn't use it in this case, because there is a specialized method exposed by the framework: Path.GetFileName. Use it like this:

var yourPath = @"C:\wamp\www\tm\23786.txt";
var filename = Path.GetFileName(yourPath);


Try Path.GetFileName(@"C:\wamp\www\tm\23786.txt");


Well, in C# land we don't like when stuff explodes, so we have

System.IO.Path.GetFileName(filename);


Simply use the Path class method GetFileName(path), such as:

var filename = System.IO.Path.GetFileName(path);

You can also just omit the System.IO qualification if you have a using System.IO.


Use System.IO.Path.GetFileName(path);


var fileName = Path.GetFileName(@"C:\wamp\www\tm\23786.txt");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜