英文:
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";
英文:
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";
答案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是您计算机上文件夹的替代名称。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论