'Double separator in the path after Path.GetDirectoryName method c#

I want get directory from blob absolute Uri: https://001.blob.core.windows.net/files/11-files.trg.

For this I use Path.GetDirectoryName method. As result I have: https:\\001.blob.core.windows.net\\files.

Why does a double separator appear and how to replace it with a single one?



Solution 1:[1]

I think Path.GetDirectoryName only works for relative paths, but not for URLs. So the first thing that comes to my mind is something like this:

var url = "https://001.blob.core.windows.net/files/11-files.trg";

var temp = new string(url.ToCharArray().Reverse().ToArray());
int index = temp.IndexOf('/');
temp = temp.Substring(index + 1 , temp.Length - index - 1);
var result = new string(temp.ToCharArray().Reverse().ToArray());

Console.WriteLine(result);
//output: https://001.blob.core.windows.net/files

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1