WYVE

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

总结:

①Path类是static类型

②常用方法

Path.GetFullPath(file) 取全路径
Path.GetFileName(file) 取文件名,包含扩展名
Path.GetFileNameWithoutExtension(file) 取文件名,不包含扩展名
Path.GetExtension(file) 取扩展名
Path.GetDirectoryName(file) 取路径名
Path.GetPathRoot(file)  取盘符
Path.Combine(file1,file2) 合并2个路径

③注意事项

a.使用前需要对参数进行判空,其次追加try catch来捕获Exception。

            string fullpath7 = null;
            if (!String.IsNullOrWhiteSpace(file))
            {
                try
                {
                    fullpath7 = Path.GetFullPath(file);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }

 

b.GetFullPath()可以将多个 / \ 合并为1个,使之格式化为合法的路径。末尾的\会保留。

c.GetDirectoryName()结果末尾没有\。

d.Combine() 不会合并多个 / \  ,第二个参数不能以/ \ 开头。

e.利用GetDirectoryName()和Combine()去掉末尾\.

string dir2 = Path.GetDirectoryName(Path.Combine(@"D:\1","Temp")); // D:\1
string dir3 = Path.GetDirectoryName(Path.Combine(@"D:\1\", "Temp")); // D:\1

 

④测试

       public static void TestPath()
        {


            string file = "App.config"; //c:\users\qqq\source\repos\StudyDesignMode\StudyDesignMode\bin\Debug\App.config


            /* 正常用法 */

            //取完整路径
            string fullpath = Path.GetFullPath(file);  //c:\users\qqq\source\repos\StudyDesignMode\StudyDesignMode\bin\Debug\App.config

            //取文件名
            string name = Path.GetFileName(@"D:\1\2\App.config"); //App.config

            //取扩展名
            string extension = Path.GetExtension(@"D:\1\2\App.config"); //.config

            //取文件名 不带扩展名
            string nameWithoutExtension = Path.GetFileNameWithoutExtension(@"D:\1\2\App.config"); //App

            //取所在文件夹
            string dir = Path.GetDirectoryName(@"D:\1\2\App.config"); //D:\1\2

            //取所在磁盘
            string root = Path.GetPathRoot(@"D:\1\2\App.config"); //D:\

            //连接路径
            string combine = Path.Combine(@"1", @"2"); // 1\2

            /* 异常用法 */

            //参数为null
            //string fullpath1 = Path.GetFullPath(null); //Exception

            //参数为空字符串
            //string fullpath2 = Path.GetFullPath("");  //Exception
            string fullpath3 = Path.GetFullPath(@"D:\\//\\//dfjk\\\\1///2\/\/\/\/\/3\");  //D:\dfjk\1\2\3\     忽略了多个 /  \ 为1个 \ 。保留了末尾的 \ 。

            //无扩展名
            string name1 = Path.GetFileName(@"D:\1\2\App"); //App 

            //只有扩展名
            string name2 = Path.GetFileName(@"D:\1\2\.config"); //.config

            //无文件名
            string name3 = Path.GetFileName(@"D:\1\2\"); // ""

            //只有盘符
            string name4 = Path.GetFileName(@"D:"); //  ""

            //参数为null
            string name5 = Path.GetFileName(null); // null

            //参数为""
            string name6 = Path.GetFileName(""); // ""


            //无扩展名
            string extension1 = Path.GetExtension(@"D:\1\2\App"); //""
                                                                  //只有扩展名
            string extension2 = Path.GetExtension(@"D:\1\2\.config"); //.config
                                                                      //无文件名
            string extension3 = Path.GetExtension(@"D:\1\2\"); // ""
                                                               //只有盘符
            string extension4 = Path.GetExtension(@"D:"); //  ""

            //参数为null
            string extension5 = Path.GetExtension(null); //  null
                                                         //参数为""
            string extension6 = Path.GetExtension(""); // ""


            //参数为null
            //string combine1 = Path.Combine(null,null); // Exception
            //string combine2 = Path.Combine("", null); // Exception

            //参数为""
            string combine3 = Path.Combine("", ""); // ""

            //多个/ \ 
            string combine4 = Path.Combine(@"///1\\\2\3", @"4"); //     ///1\\\2\3\4

            //第二个参数以/开头
            string combine5 = Path.Combine(@"///1\\\2\3", @"/4"); //    /4
            //第二个参数以\开头
            string combine6 = Path.Combine(@"///1\\\2\3", @"\4"); //   \4

            //第一个参数以\结尾
            string combine7 = Path.Combine(@"///1\\\2\3\\", @"4"); //   ///1\\\2\3\\4
            //第一个参数以/结尾
            string combine8 = Path.Combine(@"///1\\\2\3/", @"4"); //   ///1\\\2\3/4

            //第二个参数以/开头
            string combine9 = Path.Combine(@"///1\\\2\3\", @"/4"); //   /4
            //第二个参数以\开头
            string combine10 = Path.Combine(@"///1\\\2\3\", @"\4"); //   \4

            //取所在文件夹
            string dir1 = Path.GetDirectoryName(@"D:\1\2\"); //D:\1\2

            string dir2 = Path.GetDirectoryName(Path.Combine(@"D:\1","Temp"));  //  D:\1
            string dir3 = Path.GetDirectoryName(Path.Combine(@"D:\1\", "Temp")); //  D:\1

        }

补充:

            string dir4 = Path.GetDirectoryName(@"\\192.168.0.2\share\file_1.txt"); // \\192.168.0.2\share
            string root1 = Path.GetPathRoot(@"\\192.168.0.2\share\file_1.txt");  //  \\192.168.0.2\share
            string root2 = Path.GetPathRoot(@"192.168.0.2\share\file_1.txt");  //  ""
            string root3 = Path.GetPathRoot(@"//192.168.0.2\share\file_1.txt"); //  \\192.168.0.2\share
            string root4 = Path.GetPathRoot(@"//192.168.0.2\share\1\2file_1.txt"); //  \\192.168.0.2\share

 

posted on 2018-08-10 14:01  WYVE  阅读(5267)  评论(0编辑  收藏  举报