开发者

Converting relative paths to absolute paths C#

开发者 https://www.devze.com 2023-01-07 02:18 出处:网络
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.

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开发者_JAVA技巧\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?


You can use the Uri class to combine them:

Uri root = new Uri(@"\\server\webroot\folder\foo\bar\mew\", UriKind.Absolute);
Uri relative = new Uri("../../../images/arrow.gif", UriKind.Relative);

Uri comb = new Uri(root, relative);


I assume you're using ASP.NET here. In this case, I think you simply want the Server.MapPath function to return the actual physical URI of the file.

var absoluteUrl = this.Server.MapPath("../../../images/arrow.gif");
// absoluteUrl = "\\server\webroot\folder\images\arrow.gif"

(this refers to the current page of course. You can always use HttpContext.Current.Server instead, 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.Path should do the job I would think:

var absoluteUri = Path.GetFullPath(Path.Combine("\\server\webroot\folder\",
    "../../../images/arrow.gif"));


The Path class provides the method GetFullPath(string path)

For example

Path.GetFullPath(@"\\server\webroot\folder\foo\bar\mew\..\..\..\images\arrow.gif")

returns

@"\\server\webroot\folder\images\arrow.gif"


Check out the obvious candidates:

  • Path
  • DirectoryInfo

I bet they have some method to do this. Guess: Create a new DirectoryInfo object for your path and then check the properties - probably canonical path in there somewhere...


See answer here:

https://stackoverflow.com/a/42939289/2338477

It's better because it does not require path to Uri remapping, and GetFullPath in a turn involves file system operations, and because of this answer will be probably faster than other versions here. (Pure string manipulation)

0

精彩评论

暂无评论...
验证码 换一张
取 消