相对路径在C#中的绝对路径? [英] Relative path to absolute path in C#?

查看:897
本文介绍了相对路径在C#中的绝对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含HREF图像文件路径的XML文件(例如...... \images\image.jpg)。该HREF中包含相对路径。现在,我需要的HREF中提取到的图像,并把他们变成在文件系统中的绝对路径。

I have xml files that contain href file paths to images (e.g. "....\images\image.jpg"). The hrefs contain relative paths. Now, I need to extract the hrefs to the images and turn them into absolute paths in the file system.

我了解GetFullPath方法,但我想它和它似乎只从使用currentDirectory集,这似乎是C的工作:所以我不看我怎么可以使用它。而且,我仍然有一个包含HREF中的文件,并在href相对路径的绝对路径,所以既然是一个简单的任务对我来说,基于绝对路径指望后面的数字...... \部分包含的文件,似乎必须有办法做到这一点编程为好。

I know about the GetFullPath method, but I tried it and it only seems to work from the CurrentDirectory set, which appears to be C: so I don't see how I could use that. And still, I have the absolute path of the file containing the hrefs, and the href relative paths, so since it is a simple task for me to count back the number of "....\" parts based on the absolute path of the containing file, it seems there must be a way to do this programmatically as well.

我希望有一些简单的方法,我只是不知道!任何想法?

I'm hoping there's some simple method I just don't know about! Any ideas?

推荐答案

假设你知道真正的目录中的XML文件居住在使用Path.Combine,如:

Assuming you know the real directory the XML file lives in use Path.Combine, e.g.

var absolute_path = Path.Combine(directoryXmlLivesIn, "..\images\image.jpg");

如果你想找回与任何完整路径..的崩溃,那么你可以使用:

If you want to get back the full path with any ..'s collapsed then you can use:

Path.GetFullPath((new Uri(absolute_path)).LocalPath);

这篇关于相对路径在C#中的绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆