引言

 

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

 

解决问题方法

 

自己写代码解决:

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. Web 上传图片加水印

    上传图片加水印 需要使用控件FileUpload 上传按钮Image控件展示上传的图片,页面中拖入三个控件 <form id="form1" runat="serv ...

  2. 解决.NET WebService引用后添加HTTP Header的问题

    麻蛋,搜索了好久,找到的都是对soap header的操作,不是对WebService的HTTP Header的操作,这是两种不同的概念,平常我们发起的WebService请求走的都是http通信协议 ...

  3. 移动端API架构 统一Proxy还是各自为政?

    今天首先回答上一篇的问题: 为什么APP通过运营商接入网络,连通率会那么差? 1. 域名缓存问题 运营商的localdns会缓存域名的解析结果,不向权威DNS递归查询解析 为什么要这么干呢? 1)运营 ...

  4. SQL Server:APPLY表运算符

    SQL Server 2005(含)以上版本,新增了APPLY表运算,为我们日常查询带来了极大的方便. 新增的APPLY表运算符把右表表达式应用到左表表达式中的每一行.它不像JOIN那样先计算那个表表 ...

  5. PHP正则表达式

    1.PHP中两个常用的正则函数 a.preg_match 正则函数,以perl语言为基础 语法:preg_match( mode,string subject,array matches) 说明:mo ...

  6. liunx命令

    关机 (系统的关机.重启以及登出 ) shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours:minute ...

  7. 对hashmap,hashset,hashtable的理解

    1.首先先理一下java的集合关系,Collection和Map接口是所有集合接口的根结点,其他集合都直接或者间接的实现了他们中的一个:collection下有:list(元素可重复)和set(不可重 ...

  8. 2016年4月最佳的20款 jQuery 插件推荐

    这个列表包括20个我们觉得是最有用的免费的 jQuery 插件,它们都是最具创新性和最省时省力的解决方案,很多都是现代化的设计和开发中碰到的问题的处理方案.如果你熟悉下面列出的任何插件,请与我们的读者 ...

  9. jQuery鼠标经过显示大图

    效果:http://keleyi.com/keleyi/phtml/image/8.htm 以下是完整代码: <!DOCTYPE html> <html lang="en& ...

  10. jquery选项卡

    用jquery实现选项卡功能 css部分: html部分: 记得一定要引入jquery文件 jquery部分: