Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path functions for URL

Tags:

c#

path

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

Example of my path:

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

I tried to do the job with Path functions and in the end just replaced the separator.

I've found that the GetDirectoryName function does not correctly replace the slashes:

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

Like you see one slash is lost.

How can I cause the Path functions to use the 'alternative' separator?

Can I use another class with the same functionality?

like image 590
Ivan Avatar asked Apr 11 '11 16:04

Ivan


People also ask

What is the path for URL?

The path refers to the exact location of a page, post, file, or other asset. It is often analogous to the underlying file structure of the website. The path resides after the hostname and is separated by “/” (forward slash).

What are the functions of URL?

URL stands for Uniform Resource Locator, and is used to specify addresses on the World Wide Web. A URL is the fundamental network identification for any resource connected to the web (e.g., hypertext pages, images, and sound files). The protocol specifies how information from the link is transferred.

Can a file path be a URL?

file is a registered URI scheme (for "Host-specific file names"). So yes, file URIs are URLs.

What are the 3 parts to a URL route?

To recap, these are the three basic elements of a website URL: The protocol – HTTP or HTTPS. The domain name (including the TLD) that identifies a site. The path leading to a specific web page.


1 Answers

I'm afraid GetDirectoryName, GetFileName, Combine,etc. use Path.DirectorySeparatorChar in the definition and you want Path.AltDirectorySeparatorChar.

And since Path is a sealed class, I think the only way to go about is string replacement.You can replace Path.DirectorySeparatorChar('\') with Path.AltDirectorySeparatorChar('/') and Path.VolumeSeparatorChar(':') with ":/"

like image 76
Bala R Avatar answered Sep 23 '22 23:09

Bala R