Path Functions for URL

I want to use the functions of the class the Path ( GetDirectoryName, GetFileName, Combineetc.) with paths in the URL format with a slash (/).

An example of my path:

"xxx://server/folder1/folder2/file"

I tried to complete the task using functions Pathand ended up just replacing the delimiter.

I found that the function GetDirectoryNameincorrectly replaces slashes:

Path.GetDirectoryName(@"xxx://server/folder/file") -> @"xxx:\server\folder"

As you can see, one slash is lost.

How can I make Path functions use an "alternate" separator?

Is it possible to use another class with the same functionality?

+5
source share
4 answers

GetDirectoryName, GetFileName, Combine .. Path.DirectorySeparatorChar , Path.AltDirectorySeparatorChar.

Path , , - . Path.DirectorySeparatorChar ('\') Path.AltDirectorySeparatorChar ('/') Path.VolumeSeparatorChar (': ') ":/"

+5

GetDirectoryName()

pageRoot = uri.Remove(uri.LastIndexOf('/') + 1);
+3

System.Uri, System.UriBuilder ( ) System.UriParser()?

+1

If the URI is the local URI of the form file file://whatever, then you can call string path = new Uri(whatever).LocalPathand call methods Pathon it. If you cannot guarantee that Uri is on a local path, you cannot guarantee that Uri components correspond to machines, folders, files, extensions, usage directories, separators, or anything else.

0
source

All Articles