zoukankan      html  css  js  c++  java
  • 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:usersqqqsource
    eposStudyDesignModeStudyDesignModeinDebugApp.config
    
    
                /* 正常用法 */
    
                //取完整路径
                string fullpath = Path.GetFullPath(file);  //c:usersqqqsource
    eposStudyDesignModeStudyDesignModeinDebugApp.config
    
                //取文件名
                string name = Path.GetFileName(@"D:12App.config"); //App.config
    
                //取扩展名
                string extension = Path.GetExtension(@"D:12App.config"); //.config
    
                //取文件名 不带扩展名
                string nameWithoutExtension = Path.GetFileNameWithoutExtension(@"D:12App.config"); //App
    
                //取所在文件夹
                string dir = Path.GetDirectoryName(@"D:12App.config"); //D:12
    
                //取所在磁盘
                string root = Path.GetPathRoot(@"D:12App.config"); //D:
    
                //连接路径
                string combine = Path.Combine(@"1", @"2"); // 12
    
                /* 异常用法 */
    
                //参数为null
                //string fullpath1 = Path.GetFullPath(null); //Exception
    
                //参数为空字符串
                //string fullpath2 = Path.GetFullPath("");  //Exception
                string fullpath3 = Path.GetFullPath(@"D:\//\//dfjk\\1///2/////3");  //D:dfjk123     忽略了多个 /   为1个  。保留了末尾的  。
    
                //无扩展名
                string name1 = Path.GetFileName(@"D:12App"); //App 
    
                //只有扩展名
                string name2 = Path.GetFileName(@"D:12.config"); //.config
    
                //无文件名
                string name3 = Path.GetFileName(@"D:12"); // ""
    
                //只有盘符
                string name4 = Path.GetFileName(@"D:"); //  ""
    
                //参数为null
                string name5 = Path.GetFileName(null); // null
    
                //参数为""
                string name6 = Path.GetFileName(""); // ""
    
    
                //无扩展名
                string extension1 = Path.GetExtension(@"D:12App"); //""
                                                                      //只有扩展名
                string extension2 = Path.GetExtension(@"D:12.config"); //.config
                                                                          //无文件名
                string extension3 = Path.GetExtension(@"D:12"); // ""
                                                                   //只有盘符
                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\23", @"4"); //     ///1\234
    
                //第二个参数以/开头
                string combine5 = Path.Combine(@"///1\23", @"/4"); //    /4
                //第二个参数以开头
                string combine6 = Path.Combine(@"///1\23", @"4"); //   4
    
                //第一个参数以结尾
                string combine7 = Path.Combine(@"///1\23\", @"4"); //   ///1\23\4
                //第一个参数以/结尾
                string combine8 = Path.Combine(@"///1\23/", @"4"); //   ///1\23/4
    
                //第二个参数以/开头
                string combine9 = Path.Combine(@"///1\23", @"/4"); //   /4
                //第二个参数以开头
                string combine10 = Path.Combine(@"///1\23", @"4"); //   4
    
                //取所在文件夹
                string dir1 = Path.GetDirectoryName(@"D:12"); //D:12
    
                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.2sharefile_1.txt"); // \192.168.0.2share
                string root1 = Path.GetPathRoot(@"\192.168.0.2sharefile_1.txt");  //  \192.168.0.2share
                string root2 = Path.GetPathRoot(@"192.168.0.2sharefile_1.txt");  //  ""
                string root3 = Path.GetPathRoot(@"//192.168.0.2sharefile_1.txt"); //  \192.168.0.2share
                string root4 = Path.GetPathRoot(@"//192.168.0.2share12file_1.txt"); //  \192.168.0.2share
  • 相关阅读:
    「UVA12293」 Box Game
    「CF803C」 Maximal GCD
    「CF525D」Arthur and Walls
    「CF442C」 Artem and Array
    LeetCode lcci 16.03 交点
    LeetCode 1305 两棵二叉搜索树中的所有元素
    LeetCode 1040 移动石子直到连续 II
    LeetCode 664 奇怪的打印机
    iOS UIPageViewController系统方法崩溃修复
    LeetCode 334 递增的三元子序列
  • 原文地址:https://www.cnblogs.com/wyvern0618/p/9454627.html
Copyright © 2011-2022 走看看