简体   繁体   English

Path.Combine中的非法字符

[英]illegal characters in Path.Combine

I have an ftp path that looks like this: 我有一个看起来像这样的ftp路径:
ftp://my|user|name:mypassword@example.com/Test/file.txt FTP://我|用户|名:mypassword@example.com/Test/file.txt

I'm trying to use two functions from Path : 我正在尝试使用Path两个函数:
1. Path.GetDirectoryName 1. Path.GetDirectoryName
2. Path.Combine 2. Path.Combine

Both return "illegal character in path". 两者都返回“路径中的非法字符”。
What is the best solution to solve this problem? 解决此问题的最佳解决方案是什么? I need to get directory and I need to combine it with different file url. 我需要获取目录,并且需要将其与其他文件url结合在一起。

Use Uri class to extract Path portion, than use Path class to manipulate it. 使用Uri类提取Path部分,而不是使用Path类进行操作。

Use UriBuilder to construct it back. 使用UriBuilder将其重新构造。

var fullPath = 
  new Uri(@"ftp://my|user|name:mypassword@example.com/Test/file.txt")
     .AbsolutePath;

To extract the path and query string from a uri, you can use new Uri(someString).PathAndQuery as seen in answer Get url parts without host 为了从一个URI路径和查询字符串,你可以使用new Uri(someString).PathAndQuery在回答看到获取URL部分,而无需主机

To combine Url or Uri, you may use the answer https://stackoverflow.com/a/23399048/3481183 要合并Url或Uri,您可以使用答案https://stackoverflow.com/a/23399048/3481183

The Uri.Combine function described in that answer was tested for combining ftp parts as well. 该答案中描述的Uri.Combine函数也已测试过是否可以组合ftp部分。

在此处输入图片说明

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

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