zoukankan      html  css  js  c++  java
  • .Net中绝对路径与相对路径(如“..\a.txt”)的拼合方法

    引言

     cb在做文件路径处理时,经常需要对一个路径的相对路径进行操作,那么如何拼合相对路径以生成新的绝对路径呢?

    Path.Combine()方法

    我们知道System.IO.Path是专门用来处理路径的静态类,它有一个Combine()方法就是用于拼接路径的,我们来测试一下其拼接效果。

    我们使用一个命令行程序进行测试,这里要测试相对于文件C:\abc\123\avatar.html的一系列相对路径,测试代码如下:

    class Program

    {

        static string path = @"C:\abc\123\avatar.html";

     

        static void Main(string[] args)

        {

            Console.WriteLine(path);

            Console.WriteLine("输入相对路径以完成合并:");

            Console.WriteLine();

            while (true)

            {

                Console.WriteLine("合并为:"+合并路径(Console.ReadLine()));

                Console.WriteLine();

            }

        }

     

        private static string 合并路径(string p)

        {

            return Path.Combine(Path.GetDirectoryName(path), p);

        }

    }

    其中“合并路径”方法的功能是先获取文件的所在目录,再与相对路径拼合。

    测试结果:

    image

    可以看到,常规的路径拼合没有问题,但是输入“..\”就没有被正确处理为上级目录,而是直接进行了合并,这不是我期望看到的。

    怎样做才能支持“..\”形式的相对路径呢?

    利用Uri对象的构造函数

    我发现Uri对象在构造时可以传入一个基于的Uri及一个相对路径以构造为新的Uri,而我们可以以“file://……”的形式来表示本地文件路径,让我们改动一下代码,进行一下相对Uri的拼合测试。

    改动后的代码:

    class Program

    {

        //static string path = @"C:\abc\123\avatar.html";

        static string path = @"file:///C:/abc/123/avatar.html";

     

        static void Main(string[] args)

        {

            Console.WriteLine(path);

            Console.WriteLine("输入相对路径以完成合并:");

            Console.WriteLine();

            while (true)

            {

                //Console.WriteLine("合并为:" + 合并路径(Console.ReadLine()));

                Console.WriteLine("合并为:" + 合并Uri(Console.ReadLine()));

                Console.WriteLine();

            }

        }

     

        private static string 合并路径(string p)

        {

            return Path.Combine(Path.GetDirectoryName(path), p);

        }

     

        private static string 合并Uri(string p)

        {

            return new Uri(new Uri(path), p).AbsoluteUri;

        }

    }

    测试结果:

    image

    好极了,完美支持“../”形式的相对路径!

    完善

    那么接下来的工作就是将路径转换为Uri形式,然后拼合相对路径,再转换回路径形式就可以了。

    转换的时候仅仅是采取字符串处理的方法,改动后的代码如下:

    class Program

    {

        static string path = @"C:\abc\123\avatar.html";

     

        static void Main(string[] args)

        {

            Console.WriteLine(path);

            Console.WriteLine("输入相对路径以完成合并:");

            Console.WriteLine();

            while (true)

            {

                Console.WriteLine("合并为:" + 合并路径(Console.ReadLine()));

                Console.WriteLine();

            }

        }

     

        private static string 合并路径(string p)

        {

            return new Uri(new Uri("file:///" + path.Replace("\\", "/")), p.Replace("\\", "/")).AbsoluteUri.Substring(8).Replace("/", "\\");

        }

    }

    测试结果:

    image

    结语

    结果很令人满意,但我总觉得这是个土方子、山寨办法,谁有更简便、正统点的方法吗

    感谢天方这么快就提出了正统的写法:Path.GetFullPath(Path.Combin(@"C:\a\b\c","..\b.text"));

    我之前找了那么久,又折腾那么久,才弄出个山寨的来,实在汗颜啊,呵呵。 

    下载

    本文的XPS版本:http://www.uushare.com/user/icesee/file/2185677

  • 相关阅读:
    3.Appium运行时出现:Original error: Android devices must be of API level 17 or higher. Please change your device to Selendroid or upgrade Android on your device
    3.Python连接数据库PyMySQL
    2.Python输入pip命令出现Unknown or unsupported command 'install'问题解决
    2.Linux下安装Jenkins
    5.JMeter测试mysql数据库
    Android 4学习(7):用户界面
    Android 4学习(6):概述
    Android 4学习(5):概述
    Android 4学习(4):概述
    Android 4学习(3):概述
  • 原文地址:https://www.cnblogs.com/SkyD/p/1595637.html
Copyright © 2011-2022 走看看