欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-12 08:28:31
...
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:\