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

string path = "C:\dir1\dir2\foo.txt";  
string str = "GetFullPath:" + Path.GetFullPath(path) + "
";  
str += "GetDirectoryName:" + Path.GetDirectoryName(path) + "
"; 
str += "GetFileName:" + Path.GetFileName(path) + "
";  
str += "GetFileNameWithoutExtension:" + Path.GetFileNameWithoutExtension(path) + "
";  
str += "GetExtension:" + Path.GetExtension(path) + "
"; 
str += "GetPathRoot:" + Path.GetPathRoot(path) + "
"; 
MessageBox.Show(str); 
结果:  
GetFullPath:C:dir1dir2foo.txt 
GetDirectoryName:C:dir1dir2 
GetFileName:foo.txt  
GetFileNameWithoutExtension:foo 
GetExtension:.txt 
GetPathRoot:C:

  

原文地址:https://www.cnblogs.com/dengshiwei/p/3999710.html