简体   繁体   中英

illegal characters in Path.Combine

I have an ftp path that looks like this:
ftp://my|user|name:mypassword@example.com/Test/file.txt

I'm trying to use two functions from Path :
1. Path.GetDirectoryName
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.

Use Uri class to extract Path portion, than use Path class to manipulate it.

Use UriBuilder to construct it back.

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

To combine Url or Uri, you may use the answer https://stackoverflow.com/a/23399048/3481183

The Uri.Combine function described in that answer was tested for combining ftp parts as well.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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