引言

 

在项目中常需要将绝对路径,转换为相对路径,来增加程序相关配置的的灵活性(不用因为整体挪个位置就导致我们的程序不能正常工作)

 

解决问题方法

 

自己写代码解决:

private string RelativePath(string absolutePath, string relativeTo)
{
string[] absoluteDirectories = absolutePath.Split('\\');
string[] relativeDirectories = relativeTo.Split('\\'); //Get the shortest of the two paths
int length = absoluteDirectories.Length < relativeDirectories.Length ? absoluteDirectories.Length : relativeDirectories.Length; //Use to determine where in the loop we exited
int lastCommonRoot = -1;
int index; //Find common root
for (index = 0; index < length; index++)
if (absoluteDirectories[index] == relativeDirectories[index])
lastCommonRoot = index;
else
break; //If we didn't find a common prefix then throw
if (lastCommonRoot == -1)
throw new ArgumentException("Paths do not have a common base"); //Build up the relative path
StringBuilder relativePath = new StringBuilder(); //Add on the ..
for (index = lastCommonRoot + 1; index < absoluteDirectories.Length; index++)
if (absoluteDirectories[index].Length > 0)
relativePath.Append("..\\"); //Add on the folders
for (index = lastCommonRoot + 1; index < relativeDirectories.Length - 1; index++)
relativePath.Append(relativeDirectories[index] + "\\");
relativePath.Append(relativeDirectories[relativeDirectories.Length - 1]); return relativePath.ToString();
}

 

通过C#中URI类来解决:

System.Uri uri1 = new Uri(@"C:\filename.txt");
System.Uri uri2 = new Uri(@"C:\mydirectory\anotherdirectory\"); Uri relativeUri = uri2.MakeRelativeUri(uri1); Console.WriteLine(relativeUri.ToString());

 

 

参考文献

 

http://msdn.microsoft.com/en-us/library/system.uri.makerelativeuri(v=vs.110).aspx

 

相对路径

C# 将绝对路径转换为相对路径的更多相关文章

  1. MVC将服务器端的物理路径转换为服务器路径

    以图片为例 后台Controller.cs public FileResult ImageUrl(string file) { return File("物理路径"+file, & ...

  2. C#中相对路径转换为绝对路径的方法

    第一种方法:使用System.Web类,System.Web.HttpContext.Current.Server.MapPath('相对路径');它还可以写成下面这种先声明空间,然后再使用函数的方式 ...

  3. C# url 路径转换 相对路径 转换为 绝对路径

    用C#写爬虫时候,比较实用的一项技巧. /// <summary> /// 格式化URL函数 urlX 传入相对URL objurl 传入绝对基URL 基URL 一定要带HTTP:// / ...

  4. PHP 相对路径转换为绝对路径 realpath

    * 相对路径 -> 绝对路径 realpath <?php /** * @param string $in_rel: relative directory * @param string ...

  5. c# 虚拟路径转换为绝对路径

    /// <summary> /// 解析相对Url /// </summary> /// <param name="relativeUrl">相 ...

  6. web中绝对路径换虚拟路径

    最近在做一个web项目,将图片上传到服务器后,再访问时拿到的是绝对路劲,而需要的是虚拟路劲.经过一番折腾找到了下列方法可以直接转换. /// <summary>        /// 将W ...

  7. File IO(NIO.2):路径类 和 路径操作

    路径类 Java SE 7版本中引入的Path类是java.nio.file包的主要入口点之一.如果您的应用程序使用文件I / O,您将需要了解此类的强大功能. 版本注意:如果您有使用java.io. ...

  8. Javascript 将图片的绝对路径转换为base64编码

    Javascript将图片的绝对路径转换为base64编码 我们可以使用canvas.toDataURL的方法将图片的绝对路径转换为base64编码:在这我们引用的是淘宝首页一张图片如下: var i ...

  9. PHP文本路径转换为链接文字

    <?php /** * 文本路径转换为有链接的文字 * @param string $str 转换内容 * @return string */ function urlToLink($str) ...

随机推荐

  1. 关于本月第一天,本月最后一天的SQL代码

    DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)  --返回本月第一天 dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate() ...

  2. hotcss用法

    1.0 GitHub下载地址: https://github.com/imochen/hotcss 或者我的百度网盘:http://pan.baidu.com/s/1pKlLqHX 使用方法看demo ...

  3. 自定义分页控件PageList

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  4. oracle实用sql之将逗号分割的字符串分割多个列

    select regexp_substr('a,b,c,','[^,]+',1,rownum) from dual connect by rownum<=length(regexp_replac ...

  5. 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合

    [源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...

  6. csharp: Oracle Stored Procedure DAL using ODP.NET

    paging : http://www.codeproject.com/Articles/44858/Custom-Paging-GridView-in-ASP-NET-Oracle https:// ...

  7. JAVAScript柯里化、部分应用参数终极理解

    一.柯里化 在定义柯里化.部分应用参数的概念前,首先必须对闭包有深入的了解和定义,闭包一句话说清楚:函数返回值为函数. 柯里化的定义:将多参函数分解为按步骤接受单个参数的函数,如下代码: var mo ...

  8. 怎么才能算大项目(Application),大的衡量?

    最近在思考一个问题,就是怎么才算一个大型项目,大的衡量有哪些东西?为什么要搞清这个问题呢,是因为在实际的开发过程中,如果知道这是个多大的项目,那就有各种相应的解决方案(我们称之为套路的东西)就可以使用 ...

  9. 搭建基于 STM32 和 rt-thread 的开发平台

    我们需要平台 如果说,SharePoint 的价值之一在于提供了几乎开箱即用的 innovation 环境,那么,智能设备的开发平台也一样.不必每次都从头开始,所以需要固定的工作室和开发平台作为创新的 ...

  10. 发布有礼!2015 Autodesk程序商店有奖发布活动拉开序幕

    您是不是有希望您的 Autodesk 产品应用程序有更多的用户?您是不是正在寻求更广阔的市场机会?您是不是在激荡人心的云时代大潮中有许多奇思妙想没有小试身手? 来吧,来参加Autodesk应用程序发布 ...