相对路径转绝对路径

参考:点击打开链接,  有改动。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication2
{
    class Program
    {
        public class MyPath
        {
            public string AbsolutePath { get; set; }
            public string RelativePath { get; set; }
            public string Result
            {
                get
                {
                    return GetAbsPath(this.AbsolutePath, this.RelativePath);
                }
            }

            public override string ToString()
            {
                return string.Format("AbsolutePath : {0},\r\nRelativePath : {1},\r\nResult: {2}\r\n------------\r\n",
                    this.AbsolutePath
                    , this.RelativePath
                    , this.Result
                    );
            }
        }

        static void Main(string[] args)
        {
            List<MyPath> list = new List<MyPath>() { 
                new MyPath(){ AbsolutePath = @"C:\Program Files\Internet Explorer", RelativePath=@"..\..\..\..\..\..\..\..\..\..\getTime.ps1" },
                new MyPath(){ AbsolutePath = @"C:\Program Files\Internet Explorer", RelativePath=@"..\mypath\getTime.ps1" },
                new MyPath(){ AbsolutePath = @"C:\Program Files\Internet Explorer", RelativePath=@"..\TestPath\subpath" },
                new MyPath(){ AbsolutePath = @"C:\Program Files\Internet Explorer", RelativePath=@"..\" },
                new MyPath(){ AbsolutePath = @"C:\Program Files\Internet Explorer", RelativePath=@".\" },
                new MyPath(){ AbsolutePath = @"C:\Program Files\Internet Explorer", RelativePath=@".\xx.bat" },
            };

            list.ForEach((item)=> {
                Console.WriteLine(item.ToString());
            });

            Console.Read();
        }

        private static string GetAbsPath(string absolutePath, string relativePath)
        {
            string fileName = Path.GetFileName(relativePath);
            relativePath = relativePath.Substring(0, relativePath.Length - fileName.Length);
            string splicingResult = absolutePath;

            if (!Path.IsPathRooted(relativePath) && relativePath!=@".\" )
            {
                //匹配相对路径,匹配需要向上推的目录层数
                Regex regex = new Regex(@"^\\|([..]+)");
                int backUp = regex.Matches(relativePath).Count;
                List<string> pathes = absolutePath.Split("\\".ToCharArray()).ToList();
                if (pathes.Count < backUp)
                {
                    pathes.RemoveRange(0, pathes.Count);
                }
                else
                {
                    pathes.RemoveRange(pathes.Count - backUp, backUp);
                }
                //匹配文件名,匹配需要附加的目录层数
                regex = new Regex(@"^\\|([a-zA-Z0-9]+)");
                MatchCollection matches = regex.Matches(relativePath);
                foreach (Match match in matches)
                {
                    pathes.Add(match.Value);
                }
                //驱动器地址取绝对路径中的驱动器地址
                if (pathes.Count == 0) 
                {
                    splicingResult = Path.Combine(splicingResult, Path.GetPathRoot(absolutePath));
                }
                else
                {
                    pathes[0] = Path.GetPathRoot(absolutePath);
                    foreach (string p in pathes)
                    {
                        splicingResult = Path.Combine(splicingResult, p);
                    }
                }
            }
            return splicingResult + "\\" + fileName;
        }
    }
}



猜你喜欢

转载自blog.csdn.net/yenange/article/details/80499196