C# 将绝对路径转换为相对路径
引言
在项目中常需要将绝对路径,转换为相对路径,来增加程序相关配置的的灵活性(不用因为整体挪个位置就导致我们的程序不能正常工作)
解决问题方法
自己写代码解决:
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# 将绝对路径转换为相对路径的更多相关文章
- MVC将服务器端的物理路径转换为服务器路径
以图片为例 后台Controller.cs public FileResult ImageUrl(string file) { return File("物理路径"+file, & ...
- C#中相对路径转换为绝对路径的方法
第一种方法:使用System.Web类,System.Web.HttpContext.Current.Server.MapPath('相对路径');它还可以写成下面这种先声明空间,然后再使用函数的方式 ...
- C# url 路径转换 相对路径 转换为 绝对路径
用C#写爬虫时候,比较实用的一项技巧. /// <summary> /// 格式化URL函数 urlX 传入相对URL objurl 传入绝对基URL 基URL 一定要带HTTP:// / ...
- PHP 相对路径转换为绝对路径 realpath
* 相对路径 -> 绝对路径 realpath <?php /** * @param string $in_rel: relative directory * @param string ...
- c# 虚拟路径转换为绝对路径
/// <summary> /// 解析相对Url /// </summary> /// <param name="relativeUrl">相 ...
- web中绝对路径换虚拟路径
最近在做一个web项目,将图片上传到服务器后,再访问时拿到的是绝对路劲,而需要的是虚拟路劲.经过一番折腾找到了下列方法可以直接转换. /// <summary> /// 将W ...
- File IO(NIO.2):路径类 和 路径操作
路径类 Java SE 7版本中引入的Path类是java.nio.file包的主要入口点之一.如果您的应用程序使用文件I / O,您将需要了解此类的强大功能. 版本注意:如果您有使用java.io. ...
- Javascript 将图片的绝对路径转换为base64编码
Javascript将图片的绝对路径转换为base64编码 我们可以使用canvas.toDataURL的方法将图片的绝对路径转换为base64编码:在这我们引用的是淘宝首页一张图片如下: var i ...
- PHP文本路径转换为链接文字
<?php /** * 文本路径转换为有链接的文字 * @param string $str 转换内容 * @return string */ function urlToLink($str) ...
随机推荐
- error C2664: 'BOOL (PCERT_SELECT_STRUCT_A)' : cannot convert parameter 1 from 'CERT_SELECT_STRUCT *' to 'PCERT_SELECT_STRUCT_A'
1. 编译c++程序出现标题所示错误 2. 解决方法:更改编码设置
- Storm的ack机制在项目应用中的坑
正在学习storm的大兄弟们,我又来传道授业解惑了,是不是觉得自己会用ack了.好吧,那就让我开始啪啪打你们脸吧. 先说一下ACK机制: 为了保证数据能正确的被处理, 对于spout产生的每一个tup ...
- 四步让你的网站秒开,wordpress框架为例子,其他框架道理类似
我这里以wordpress框架制作的网站为例子,效果可以看看我的网站,香港的垃圾主机199一年2M带宽,速度也能秒开,不信试试效果33小游戏 我的是wordpress制作的网站,大家都知道WP各种臃肿 ...
- 缓存、队列(Memcached,Redis,rabbitMQ)
一.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...
- AngularJS中的指令全面解析(转载)
说到AngularJS,我们首先想到的大概也就是双向数据绑定和指令系统了,这两者也是AngularJS中最为吸引人的地方.双向数据绑定呢,感觉没什么好说的,那么今天我们就来简单的讨论下AngularJ ...
- LINUX下C-C++类软件的诊断
一般C/C++写程序由于内存.指针等问题在后期的过程都一般会遇到内存等资源泄露.崩溃等问题,对于这些问题,一般从哪个角度解决呢? 下面从几个角度进行总结: 内存泄露 首先需要注意一个问题是top或者h ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q105-Q108)
Question 105 You are designing a SharePoint 2010 application that contains a single list named Us ...
- iOS之设置头像(访问系统相册、本地上传)
1. UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: ...
- Android项目实战(二十六):蓝牙连接硬件设备开发规范流程
前言: 最近接触蓝牙开发,主要是通过蓝牙连接获取传感器硬件设备的数据,并进行处理. 网上学习一番,现整理出一套比较标准的 操作流程代码. 如果大家看得懂,将来只需要改下 硬件设备的MAC码 和 改下对 ...
- Android屏幕适配
安卓分辨率的相关知识 http://leoray.leanote.com/post/android-resolution Android内存占用公式 系统内存 = 像素宽 * 像素高 * 像素所占用的 ...