System.IO.Path相关常用函数使用介绍

本章讲述:System.IO.Path相关常用函数使用介绍

1、GetFileName(string path):返回指定路径字符串的文件名和扩展名

string localDir = System.IO.Path.GetFileName(@"D:\360Downloads\Software\filelist.dat");

返回结果为:filelist.dat

2、GetFileNameWithoutExtension:返回不具有扩展名的指定路径字符串的文件名;
返回System.IO.Path.GetFileName(System.String) 的字符串,但不包括最后的句点 (.) 以及之后的所有字符;

string appCode = System.IO.Path.GetFileNameWithoutExtension(@"D:\360Downloads\Software\filelist.dat");

返回结果:filelist

3、Combine:将字符串或者字符串数组,组合成一个路径;注意字符串中是否含无效字符,会异常

Combine(string path1, string path2, string path3);
Combine(string path1, string path2);
Combine(string path1, string path2, string path3, string path4);
Combine(params string[] paths);

4、GetDirectoryName(string path):返回指定路径字符串的目录信息;path:文件或目录的路径

string localDir = System.IO.Path.GetDirectoryName(@"D:\360Downloads\Software\filelist.dat");

返回结果为:Software

5、GetExtension(string path):返回指定的路径字符串的扩展名

string localDir = System.IO.Path.GetExtension(@"D:\360Downloads\Software\filelist.dat");

返回结果为:dat

6、GetFullPath(string path):返回指定路径字符串的绝对路径

7、GetPathRoot(string path):获取指定路径的根目录信息;此方法不会验证路径是否存在

string localDir = System.IO.Path.GetPathRoot(@"D:\360Downloads\Software\filelist.dat");

返回结果为:D:\

string localDir = System.IO.Path.GetPathRoot(@"\Software\");

返回结果为:\

string localDir = System.IO.Path.GetPathRoot(@"filelist.dat");

返回结果为:""

8、HasExtension(string path):确定路径是否包括文件扩展名

9、HasExtension(string path):获取指示指定的路径字符串是否包含根的值

猜你喜欢

转载自blog.csdn.net/BYH371256/article/details/120410210