【问题标题】:How to create correctly escaped file:// URI in C#?如何在 C# 中创建正确转义的 file:// URI?
【发布时间】:2009-06-23 17:39:15
【问题描述】:

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

给定以下输入

C:\Data\Input Document.txt

我想买

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

我一直在用

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

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

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

【问题讨论】:

    标签: c# uri


    【解决方案1】:

    已经编码了

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

    【讨论】:

    • 谢谢,我错过了。但是,它仅适用于绝对 URI,因为此属性未针对相对 URI 设置。
    【解决方案2】:

    你试过了吗:

    new Uri(str).AbsoluteUri

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

    【讨论】:

      猜你喜欢
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      相关资源
      最近更新 更多