C# Path總結


總結:

①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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM