绝对相对路径 [英] Absolute to Relative path
本文介绍了绝对相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我碰到一个OpenFileDialog它返回到所选文件的绝对路径的字符串的文件。现在我想这条道路来定路径的相对路径(在这种情况下的路径,我的应用程序)
I'm getting a file from an OpenFileDialog which returns a string with the absolute path to the selected file. Now I want that path as a relative path to a given path (in this case the path to my application).
所以我们可以说我得到该文件的路径:
C:\myDock\programming\myProject\Properties\AssemblyInfo.cs
So let's say I get a path to the file:
c:\myDock\programming\myProject\Properties\AssemblyInfo.cs
我的应用程序位于
C:\myDock\programming\otherProject\bin\Debug\program.exe
然后我想要的结果:
。 .\..\..\myProject\Properties\AssemblyInfo.cs
推荐答案
在乌里
类有一个 MakeRelativeUri
方法,可以帮助。
The Uri
class has a MakeRelativeUri
method that can help.
public static string MakeRelative(string filePath, string referencePath)
{
var fileUri = new Uri(filePath);
var referenceUri = new Uri(referencePath);
return referenceUri.MakeRelativeUri(fileUri).ToString();
}
var result = MakeRelative(@"C:\dirName\dirName2\file.txt", @"C:\dirName\");
这篇关于绝对相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文