9

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?

0

5 Answers 5

6

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 ":/"

Sign up to request clarification or add additional context in comments.

Comments

6

For GetDirectoryName(), you can use

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

Comments

1

Have you considered using a combination of System.Uri, System.UriBuilder, and (if necessary) custom System.UriParser subclass(es)?

Comments

0

If the URI is a local file URI of the form file://whatever then you can call string path = new Uri(whatever).LocalPath and call the Path methods on it. If you cannot guarantee the Uri is to a local path, you cannot guarantee components of the Uri correspond to machines, folders, files, extensions, use directories, separator characters, or anything else.

Comments

0

Long time after...I was looking for a solution and found this topic, so i decided to make my (very simple) code

string dirRootUpdate = string.Empty;
string fileNameupdate = string.Empty;
string pathToGetUpdate = string.Empty;

string[] _f = Properties.Settings.Default.AutoUpdateServerUrl.Split('/');

for (int i = 0; i < _f.Count() - 1; i++)
{
    dirRootUpdate += _f[i];

    if (i == 0) // is the first one
    {
        dirRootUpdate += "/";
    }
    else if (i != _f.Count() - 2) // not the last one ?
    {
        dirRootUpdate += "/";
    }
}
fileNameupdate = _f[_f.Count() - 1];

the setting "Properties.Settings.Default.AutoUpdateServerUrl" contains the string to be verified

Works fine, may require some refination to look better. Hope could help someone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.