Reputation: 149
I have a problem where I am having MySQL DB where I store htmlBody
text.
Html body text is text like html. Example:
<img src='https://www.Something.com/Images/SomeImage.jpg'>
<p>This is some image</p>
Now I have winform application where I built custom control which builds up html texts from normal winform elements. Problem comes when I extract elements properties from string loaded from MySQL.
I have string like this: <img src='https://www.Something.com/Images/SomeImage.jpg'>
In my function I detect it is image and I create new custom class for it and inside that class I have 2 elements like this:
public string srcPath { get; set; }
public string srcFileName { get; set; }
To extract path and name from my url (https://www.Something.com/Images/SomeImage.jpg
) I use Path
but when I do
string filename = Path.GetDirectoryName(myUrl);
instead of https://www.Something.com/Images
I get https:\\www.Something.com\\Images
As you can see on first sight it replaced /
with \\
which I could overcome by replacing it again but after https
I had 2x // and instead replacing it each with 2x \\
it did it only once so even if I do replace it simple way it wont work since it will create string like https:/www.Something.com/Images
.
Solution for this I came with is to create custom function to overcome this which is not problem for me but I guess there must be better way of doing it.
Upvotes: 0
Views: 62
Reputation: 120440
URIs are not file paths and treating them as such inevitably will lead to problems, as you have discovered. However, the Uri
class has a Segments
property that can help with this decomposition.
Combined with UriBuilder
you can:
var uri = new Uri("http://a/b/c/d");
var ub = new UriBuilder(uri);
ub.Path = string.Concat(uri.Segments.Take(uri.Segments.Length - 1));
Console.WriteLine(ub.Uri.AbsoluteUri);
Console.WriteLine(uri.Segments.Last());
It's worth noting that the last segment of a URI isn't really a filename. The URI as a whole points to a resource.
Upvotes: 1