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 ...
随机推荐
- SQL百分比
百分比=cast(cast(count(ProvinceName)*100./ (select count(Id) from dbo.TopicCurrent where boardId=316 ...
- JAVA JDBC连接 SQLServer2012 连接失败 端口号错误
SQLServer2012的SQL Sever 网络配置 我有4个 SQLEXPRESS的协议 SQLSERVER2008的协议 MSSQLSERVER的协议 SQLSERVER2012的协议 他们都 ...
- C++Builder 笔记
1.界面窗口如何不显示标题栏? 在Form属性栏里面把BorderStyle的值设为None 2.wchar_t wchar_t是C/C++的字符类型,是一种扩展的存储方式,wchar_t类型主要用在 ...
- .NET: C#: System.Diagnostics
1. Trace & Debug 理解这两者的区别,Trace有个Listners.Add()非常好用,这里网上有个在ListBox里输出Debug和Trace信息的 using System ...
- centos python nginx uwsgi
先更新系统,并安装编译环境等等. yum update yum install python python-devel libxml2 libxml2-devel python-setuptools ...
- (转)SQL Server 2005 中的计算字段
在实际工作上遇到的问题: 在订单表中有某项商品是将“订购数量(Quantity)”乘以“单件价格(UnitCost)”等于该项商品的总价(Subtotal). 在数据表中有的列(以下皆改叫为“字段”) ...
- dataset 使用
下面有例子说明: 首先我们需要打开一个联结: string MyConnString = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:/ ...
- jvm排查工具
jps jps -mvl --查看java的ps进程. jstack 打印一个线程堆栈信息 top -H -p pid1 -> 得到占用资源大的pid2 jstack pid1 | grep & ...
- PAT乙级 1005. 继续(3n+1)猜想 (25)
1005. 继续(3n+1)猜想 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 卡拉兹(Callatz ...
- session讲解(二)——商城购物车练习
网上商城中“添加商品到购物车”是主要功能之一,所添加的商品都存到了session中,主要以二维数组的形式存储在session中,在这里我们将以买水果为例 第一:整个水果商品列表 <body> ...