I have a program that trawls html files and finds href tags, takes the string inside (the link), and converts it to the file location.
The problem comes when the href tag uses relative links, eg:
<a href="../../../images/arrow.gif"/>
In that case, my program returns:
\\server\webroot\folder\foo\bar\mew\..\..\..\images\arrow.gif
for example (because it doesn’t start with “http”, it appends the path of the file it’s in to the start).
This, obviously, can be simplified to:
\\server\webroot\folder\images\arrow.gif
Is there an object that can do this kind of simplification, or do I need to do some string parsing – and if so what’s the best way?
I assume you’re using ASP.NET here. In this case, I think you simply want the
Server.MapPathfunction to return the actual physical URI of the file.(
thisrefers to the current page of course. You can always useHttpContext.Current.Serverinstead, if that’s not available for whatever reason.)Note:
If you want to do things manually and you already have a specific string like “\server\webroot\folder\”, then the functionality of
System.IO.Pathshould do the job I would think: