在C#中的路径字符串获取文件名 [英] Get file name from a path string in C#
本文介绍了在C#中的路径字符串获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在WPF C#我的程序。我有例如以下路径:
I program in WPF C#. I have e.g. the following Path:
C:\Program Files\hello.txt
和我要输出你好从它。
的路径是从数据库字符串中提取。目前我使用下面的方法(分从路径由'\\'再拆再由'。')
The path is a string extract from database. Currently I'm using the following method (split from path by '\' then split again by a '.'):
string path = "C:\\Program Files\\hello.txt";
string[] pathArr = path.Split('\\');
string[] fileArr = pathArr.Last().Split('.');
string fileName = fileArr.Last().ToString();
它的工作原理,但我认为应该有更短,更智能的解决方案的。任何想法?
It works, but I believe there should be shorter and smarter solution to that. Any idea?
推荐答案
<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx\">Path.GetFileName
<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx\">Path.GetFileNameWithoutExtension
借助路径类是美好的。
这篇关于在C#中的路径字符串获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文