引言

 

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

 

解决问题方法

 

自己写代码解决:

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. C#学习笔记-封装

    前言 说起来惭愧,学了大半年的C#,其实最开始就接触到了封装的部分,但是一直模模糊糊的弄不清楚,也觉得没什么影响就没怎么在意,现在才开始认真的看这部分内容,看懂了过后好多东西清晰了不少,才发现封装这个 ...

  2. 自己动手C#模拟电梯的运行V1.0

    电梯调度有很多种模式,参见http://www.cnblogs.com/jianyungsun/archive/2011/03/16/1986439.html 1.1先来先服务算法(FCFS) 先来先 ...

  3. Entity Framework 数据库先行、模型先行、代码先行

    数据库先行(Database First):基于已存在的数据库,利用某些工具(如Vs提供的EF设计器)创建实体类,数据库对象与实体类的匹配关系等,你也可以手动修改这些自动生成的代码及匹配文件. 模型先 ...

  4. Android Permission 访问权限大全(转)

    程序执行需要读取到安全敏感项必需在androidmanifest.xml中声明相关权限请求, 完整列表如下: android.permission.ACCESS_CHECKIN_PROPERTIES允 ...

  5. 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue

    [源码下载] 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue 作者:weba ...

  6. jquery css事件编程 尺寸设置

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. MySQL中索引和优化的用法总结

    1.什么是数据库中的索引?索引有什么作用? 引入索引的目的是为了加快查询速度.如果数据量很大,大的查询要从硬盘加载数据到内存当中. 2.InnoDB中的索引原理是怎么样的? InnoDB是Mysql的 ...

  8. 【转】php pdo连接数据库 解决中文乱码问题(wordpress mysql 问号?? ??)

    原文链接:http://blog.csdn.net/ysydao/article/details/11002295 PHP 用pdo连接数据库时出现中文乱码问题解决办法 1.array(PDO::MY ...

  9. web项目知识整理

    一.div居中 1.margin:auto 2.left:50%:margin-left:div宽度的一半 二.一般处理程序操作session 引using System.Web.SessionSta ...

  10. jQuery实践树(2)

    上一个实践主要对jquery的ready事件进行.本实践要来使用jQuery改变页面的背景图片.可以先试试效果:http://hovertree.com/texiao/jquerytree/2/ 当你 ...