I wrote some code that reads non-hosted html files. These files are always on a hard drive or local drive and are not hosted in a web browser.
I need to locate href files that are inside the html file. For example, I might get an href that looks like this:
file://myserver/mp3s/greatsong.mp3
Since I need my c# code to get the file, I use the LocalPath property like this:
var path = new Uri("file://myserver/mp3s/greatsong.mp3").LocalPath;
Console.WriteLine(path);
The output is exactly what I want, which is this:
\\myserver\mp3s\greatsong.mp3
The previous code handles external networked drives, but sometimes the url deals with a local drive, like this:
../../greatsong.mp3
How can I get the folder name this mp3 file is in? I can assume the file is in the same drive location of the html file, meaning if the html file is in c:\temp\files\myhtml\myfile.htm
, then the greatsong.mp3 file is located in the c:\ drive.
The best I can think of to get the folder name is to read each instance of ../
to traverse the directory structure but that seems clunky.
Is there some sort of method I can use that looks something like this?
var whatIWant = new Uri("../../greatsong.mp3", "C:\").LocalPath;
The output of whatIWant would be:
C:\temp\greatsong.mp3
Thanks.