0

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.

user194076
  • 8,787
  • 23
  • 94
  • 154

2 Answers2

4

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;
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Thanks for your answer. but your code returns `/Test/file.txt` not `ftp://my|user|name:mypassword@example.com/Test/` – user194076 Mar 14 '13 at 21:01
  • @user194076, because it *is path*. I guess you want something else (i.e. your definition of "path" is not the one from [Uri RFC](www.ietf.org/rfc/rfc3986.txt) ). I believe classes I've mentioned should give you way to do what you want in safe manner. – Alexei Levenkov Mar 14 '13 at 23:44
0

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.

enter image description here

Community
  • 1
  • 1
Believe2014
  • 3,894
  • 2
  • 26
  • 46