简体   繁体   English

使用System.Uri创建相对路径

[英]Create Relative Path Using System.Uri

I am converting absolute file-system path to relative path using following code. 我正在使用以下代码将绝对文件系统路径转换为相对路径。

public static void Main(string[] args)
{
    System.Uri uri1 = new Uri(@"C:\Users\301591\Downloads\My Pics\abcd.jpg", true);
    System.Uri uri2 = new Uri(@"C:\Users\301591\", true);
    Uri relativeUri = uri2.MakeRelativeUri(uri1);
    Console.WriteLine(relativeUri.ToString());
}

The result I am getting is /Downloads/My Pics/abcd.jpg 我得到的结果是/ Downloads / My Pics / abcd.jpg

but compiler is throwing a warning, constructor I am using is marked obsolete. 但是编译器发出警告,我正在使用的构造函数被标记为过时。

Uri testUri=new Uri("string path","flag to remove escape sequences");

Is there any other way to create relative path with showing escape sequence in the result? 还有其他方法可以在结果中显示转义序列来创建相对路径吗?

I found the solution. 我找到了解决方案。

It is 它是

Console.WriteLine(Uri.UnescapeDataString(relativeUri.ToString()));

Uri.MakeRelative method is obsolete as per MSDN : http://msdn.microsoft.com/en-us/library/system.uri.makerelative.aspx 根据MSDN,Uri.MakeRelative方法已过时: http : //msdn.microsoft.com/zh-cn/library/system.uri.makerelative.aspx

So Urii.MakeRelativeUri cannot through obsolete warning http://msdn.microsoft.com/en-us/library/system.uri.makerelativeuri 因此Urii.MakeRelativeUri无法通过过时警告http://msdn.microsoft.com/en-us/library/system.uri.makerelativeuri

So please carry on using http://msdn.microsoft.com/en-us/library/system.uri.makerelativeuri with Cosntructor new Uri(@"C:\\Users\\301591\\"); 因此,请与Cosntructor new Uri(@“ C:\\ Users \\ 301591 \\”)一起使用http://msdn.microsoft.com/zh-cn/library/system.uri.makerelativeuri

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM