C#相对路径转绝对路径,绝对路径转相对路径
1.绝对路径转相对路径
绝对转相对似乎C#没有提供实现,需要自己写,这里摘选了一位博友的实现方法:
string RelativePath(string absolutePath, string relativeTo)
{
//from - www.cnphp6.com 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 = -;
int index; //Find common root
for (index = ; index < length; index++)
if (absoluteDirectories[index] == relativeDirectories[index])
lastCommonRoot = index;
else
break; //If we didn't find a common prefix then throw
if (lastCommonRoot == -)
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 + ; index < absoluteDirectories.Length; index++)
if (absoluteDirectories[index].Length > )
relativePath.Append("..\\"); //Add on the folders
for (index = lastCommonRoot + ; index < relativeDirectories.Length - ; index++)
relativePath.Append(relativeDirectories[index] + "\\");
relativePath.Append(relativeDirectories[relativeDirectories.Length - ]); return relativePath.ToString();
}
RelativePath
调用:
static void Main(string[] args)
{
var path = RelativePath(@"D:\MyProj\Release", @"D:\MyProj\Log\MyFile.txt"); Console.WriteLine(path);//print ..\Log\MyFile.txt
Console.Read();
}
2.相对路径转绝对路径
可以直接用.Net自己的Path.GetFullPath转换。用SetCurrentDirectory改变当前比较路径
static void Main(string[] args)
{
var relativePath = @"..\..\Release";
//Directory.SetCurrentDirectory(...)
Console.WriteLine(Path.GetFullPath(relativePath));
Console.Read();
}
C#相对路径转绝对路径,绝对路径转相对路径的更多相关文章
- js相对路径相关(比如:js中的路径依赖导入该js文件的路径)
问题描述: 前几天调用同事的js接口文件,在他自己的html测试页面ok,在我这边调用时出现问题. debug过程中,将该测试html移到其他位置都不行,放到原html测试页面同层次路径下是OK的. ...
- 在ros功能包CMakeLists.txt中获取所在功能包的路径 便于添加第三方库的相对路径
在 ros 功能包中要使用第三方的动态库,将其放在系统默认库路径和使用绝对路径均不可取,这样的话可移植性较差,将该功能包移到其它电脑时要重新配置依赖库的路径,太麻烦了. 于是找到下面这个方法,解决了R ...
- 洛谷 P2764 最小路径覆盖问题【最大流+拆点+路径输出】
题目链接:https://www.luogu.org/problemnew/show/P2764 题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V ...
- 使用任意的输入流(InputStream)实例,包括字符串形式的文件路径或者 file:// 的 URL 形式的文件路径来配置
mybatis – MyBatis 3 | 入门 http://www.mybatis.org/mybatis-3/zh/getting-started.html 从 XML 中构建 SqlSessi ...
- 【重构】C# VS 配置引用程序集的路径(分离exe和dll从指定路径调用)
原文:[重构]C# VS 配置引用程序集的路径(分离exe和dll从指定路径调用) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/CocoWu892 ...
- 读取web项目properties文件路径 解决tomcat服务器找不到properties路径问题
1.需求:有时候我们产品经理给我们的需求是会不断变化的,例如数量是1000现在变成500,我们不可以去改代码吧,这样很麻烦,所以就可以改配置文件properties(这个数据库链接一样),当然也有js ...
- Android修改Eclipse 中的Default debug keystore路径,以及修改android的AVD默认路径
初学android,光是配置Eclipse就走了不少弯路,班里面有很多同学的计算 机名都是写的自己的中文姓名,结果导致了AVD文件默认保存在“C:\user\<username>\.and ...
- ASP.NET 访问路径 错误提示 HTTP 错误 404.8 原来路径中包含bin目录被拒绝
HTTP 错误 404.8 - Not Found HTTP 错误 404.8 - Not Found 请求筛选模块被配置为拒绝包含 hiddenSegment 节的 URL 中的路径. 最可能的原因 ...
- Delphi 取得桌面文件夹的路径和取得我的文档的路径
Uses Windows,Registry; function GetShellFolders(strDir: string): string; const regPath = '\Software\ ...
- Windows文件路径转换为java中可识别的文件路径的转义方法,(另附转义多种格式)
ps:欢迎加qq好友:2318645572,交流学习 一:路径转化 Windows中的文件路径格式为 D:\eclipse\apache-tomcat-7.0.67\wtpwebapps\... Ja ...
随机推荐
- CSS For Bar Graphs(maybe old)
Having a working knowledge of XHTML and CSS when developing applications is a big help in knowing wh ...
- Swift游戏实战-跑酷熊猫 06 创建平台类以及平台工厂类
这节内容我们一起学习下随机长度的踩踏平台的原理是怎么样的. 要点: 平台类 我们的平台类继承于SKNode,这样就能被添加进其它节点进而显示在场景中. 它有一个方法来创建平台,这个方法接收一个包含SK ...
- SQL order by 两个字段排序
select * from emp;
- Topcoder SRM 598 div2
e,不知道为啥进不去了,这个B题贪心,从最小和最大的匹配如果超过了就只去最大的! 然后就没了- -! 这场比赛时不知为啥,string出问题了,可能是编译器挂了!0蛋- -!
- nyist 626 intersection set
http://acm.nyist.net/JudgeOnline/problem.php?pid=626 intersection set 时间限制:1000 ms | 内存限制:65535 KB ...
- C#:线程
http://www.cnblogs.com/leslies2/archive/2012/02/07/2310495.html 4.4委托类没看懂 http://www.cnblogs.com/les ...
- HDU 5000 Clone(离散数学+DP)(2014 ACM/ICPC Asia Regional Anshan Online)
Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself ...
- android xutils
http://blog.csdn.net/rishengcsdn/article/details/47279851/
- Dr.Kong的艺术品
题目 Dr.Kong设计了一件艺术品,该艺术品由N个构件堆叠而成,N个构件从高到低按层编号依次为1,2,……,N.艺术品展出后,引起了强烈的反映.Dr.Kong观察到,人们尤其对作品的高端部分评价甚多 ...
- 移动widget开发
发现Oracle----php连接有很多bug无法解决,只好转向php--连接mysql数据库,并装载了mysql两个文件,跟客户端NAVICAT_FOR_MYSQL,然后直接建表,用于测试,能够连通 ...