相对路径转绝对路径

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

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;
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值