0

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.

Bill
  • 582
  • 1
  • 7
  • 21

1 Answers1

0

Use this Html Agility Pack.

And you can use this Code to get the link/foldername:

  HtmlWeb hw = new HtmlWeb();
  HtmlDocument doc = hw.Load(/* url */);
  foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
  {

  }
  • Hi there. I think there may be a misunderstanding. I am not looking for code to locate the href links. I am looking for code that will convert "../../greatsong.mp3" to "c:\temp\greatsong.mp3". – Bill Aug 23 '16 at 18:01
  • Oh yeah i see my mistake sorry @Bill – Merlin Ellinger Aug 24 '16 at 14:30