如何在 C# 中创建正确转义的 file:// URI?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1034040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:36:55  来源:igfitidea点击:

How to create correctly escaped file:// URI in C#?

c#uri

提问by Dirk Vollmar

What would be the correct way to create a fully URL-encoded file://URI from a local path, i.e. where all special characters such as blanks etc are escaped?

file://从本地路径创建完全 URL 编码的URI的正确方法是什么,即所有特殊字符(如空格等)都被转义?

Given the following input

鉴于以下输入

C:\Data\Input Document.txt

I would like to get

我想得到

file:///C:/Data/Input%20Document.txt

I've been using

我一直在用

Uri uri = new Uri(@"C:\Data\Input Document.txt", UriKind.RelativeOrAbsolute); 

However, this results in an unescaped URI:

但是,这会导致未转义的 URI:

file:///C:/Data/Input Document.txt

采纳答案by Rony

it is already encoded

它已经被编码

uri.AbsolutePath should give you "C:/Data/Input%20Document.txt"

uri.AbsolutePath 应该给你 "C:/Data/Input%20Document.txt"

回答by Lachlan

Have you tried:

你有没有尝试过:

new Uri(str).AbsoluteUri

new Uri(str).AbsoluteUri

Not sure I understand your requirement for relative URIs...

不确定我是否理解您对相对 URI 的要求...