您的位置:首页 > 编程语言 > C#

C# 轻松获取路径中文件名、目录、扩展名等

2015-09-11 17:02 555 查看
string path = "C:\\dir1\\dir2\\foo.txt";

string str = "GetFullPath:" + Path.GetFullPath(path) + "\r\n";

str += "GetDirectoryName:" + Path.GetDirectoryName(path) + "\r\n";

str += "GetFileName:" + Path.GetFileName(path) + "\r\n";

str += "GetFileNameWithoutExtension:" + Path.GetFileNameWithoutExtension(path) + "\r\n";

str += "GetExtension:" + Path.GetExtension(path) + "\r\n";

str += "GetPathRoot:" + Path.GetPathRoot(path) + "\r\n";

MessageBox.Show(str);

结果:

GetFullPath:C:\dir1\dir2\foo.txt

GetDirectoryName:C:\dir1\dir2

GetFileName:foo.txt

GetFileNameWithoutExtension:foo

GetExtension:.txt

GetPathRoot:C:\
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: