如何在路径中转义井号(#)符号?

huangapple go评论115阅读模式
英文:

How to escape sharp(#) symbol in path?

问题

我试图从字符串中获取绝对路径。在遇到包含散字符(#)符号的路径之前,一切都正常工作。
以下是我的示例:

class Program
{
    static void Main(string[] args)
    {
        var codeBase = "file:///C:/#MyFolder/TestApp/bin/Debug/net6.0-windows/TestApp.dll";
        var uri = new Uri(codeBase);
        var path = Uri.UnescapeDataString(uri.AbsolutePath);
        Console.WriteLine(path);
    }
}

Uri.AbsolutePath返回C:\。是的,Uri.AbsolutePath不支持#符号。但在这种情况下,我该怎么做才能获取绝对路径呢?
老实说,这是我第一次看到有人在文件夹名称中使用这个符号。

英文:

I'm trying to get absolute path from string. Everything worked fine before i met path that contains sparp(#) symbol.
Here is my example:

class Program
    {
        static void Main(string[] args)
        {
            var codeBase = "file:///C:/#MyFolder/TestApp/bin/Debug/net6.0-windows/TestApp.dll";
            var uri = new Uri(codeBase);
            var path = Uri.UnescapeDataString(uri.AbsolutePath);
            Console.WriteLine(path);
        }
    }

And Uri.AbsolutePath returns C:\. Yeah Uri.AbsolutePath doesn't work with # symbol. But what sould i do in this case to get absolute path?
Honestly it's the first time i see that someone use this symbol in folder name.

答案1

得分: 2

对于Uri,您可以使用%23来转义保留的符号

var codeBase = "file:///C:/%23MyFolder/TestApp/bin/Debug/net6.0-windows/TestApp.dll";

演示 @sharplab.io

英文:

For Uri you can use the %23 to escape the reserved # symbol:

var codeBase = "file:///C:/%23MyFolder/TestApp/bin/Debug/net6.0-windows/TestApp.dll";

Demo @sharplab.io

答案2

得分: -1

你可以尝试类似这样的代码:

var codeBase = "file:///C:/#MyFolder/TestApp/bin/Debug/net6.0-windows/TestApp.dll";
var uri = new Uri(codeBase);
var path = Uri.UnescapeDataString(uri.AbsolutePath);
Console.WriteLine(path);

Uri 对你有用。

英文:

You can try some thing like this:

var codeBase = "file:///C:/#MyFolder/TestApp/bin/Debug/net6.0-windows/TestApp.dll";
    var uri = new Uri(codeBase);
    var path = Uri.UnescapeDataString(uri.AbsolutePath);
    Console.WriteLine(path);

The Uri will work for you

答案3

得分: -1

#FolderName是您计算机上文件夹的替代名称。

要获取精确的URL,请找到该文件并单击此处:
如何在路径中转义井号(#)符号?

单击此处将其转换为URL:
如何在路径中转义井号(#)符号?

英文:

#FolderName is the replacement to the folder in your pc.

To get the exact URL locate the file and click here:如何在路径中转义井号(#)符号?

clicking there will convert it into url:
如何在路径中转义井号(#)符号?

huangapple
  • 本文由 发表于 2023年8月10日 16:24:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定