C#中相对路径到绝对路径?

发布于 2024-10-14 05:09:23 字数 327 浏览 1 评论 0原文

我有包含图像的 href 文件路径的 xml 文件(例如“....\images\image.jpg”)。 href 包含相对路径。现在,我需要提取图像的 href 并将它们转换为文件系统中的绝对路径。

我知道 GetFullPath 方法,但我尝试过它,它似乎只能在 CurrentDirectory 集中工作,它似乎是 C: 所以我不知道如何使用它。而且,我仍然拥有包含 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

惯饮孤独 2024-10-21 05:09:23
string exactPath = Path.GetFullPath(yourRelativePath);

作品

string exactPath = Path.GetFullPath(yourRelativePath);

works

羁绊已千年 2024-10-21 05:09:23

假设您知道 XML 文件所在的真实目录,使用 Path.Combine,例如,

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

如果您想恢复任何 .. 折叠的完整路径,那么您可以使用:

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

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);
巡山小妖精 2024-10-21 05:09:23

这有效。

var s = Path.Combine(@"C:\some\location", @"..\other\file.txt");
s = Path.GetFullPath(s);

This worked.

var s = Path.Combine(@"C:\some\location", @"..\other\file.txt");
s = Path.GetFullPath(s);
じ违心 2024-10-21 05:09:23

这是将相对路径转换为绝对路径的最佳方式!

string absolutePath = System.IO.Path.GetFullPath(relativePath);

It's the best way to convert the Relative path to the Absolute one!

string absolutePath = System.IO.Path.GetFullPath(relativePath);
老娘不死你永远是小三 2024-10-21 05:09:23

您可以将 Path.Combine 与“基本”路径一起使用,然后对结果使用 GetFullPath。

string absPathContainingHrefs = GetAbsolutePath(); // Get the "base" path
string fullPath = Path.Combine(absPathContainingHrefs, @"..\..\images\image.jpg");
fullPath = Path.GetFullPath(fullPath);  // Will turn the above into a proper abs path

You can use Path.Combine with the "base" path, then GetFullPath on the results.

string absPathContainingHrefs = GetAbsolutePath(); // Get the "base" path
string fullPath = Path.Combine(absPathContainingHrefs, @"..\..\images\image.jpg");
fullPath = Path.GetFullPath(fullPath);  // Will turn the above into a proper abs path
残疾 2024-10-21 05:09:23

您是否尝试过 Server.MapPath 方法。这是一个例子

string relative_path = "/Content/img/Upload/Reports/59/44A0446_59-1.jpg";
string absolute_path = Server.MapPath(relative_path);
//will be c:\users\.....\Content\img\Upload\Reports\59\44A0446_59-1.jpg

Have you tried Server.MapPath method. Here is an example

string relative_path = "/Content/img/Upload/Reports/59/44A0446_59-1.jpg";
string absolute_path = Server.MapPath(relative_path);
//will be c:\users\.....\Content\img\Upload\Reports\59\44A0446_59-1.jpg
噩梦成真你也成魔 2024-10-21 05:09:23

这对我有用。

//used in an ASP.NET MVC app
private const string BatchFilePath = "/MyBatchFileDirectory/Mybatchfiles.bat"; 
var batchFile = HttpContext.Current.Server.MapPath(BatchFilePath);

This worked for me.

//used in an ASP.NET MVC app
private const string BatchFilePath = "/MyBatchFileDirectory/Mybatchfiles.bat"; 
var batchFile = HttpContext.Current.Server.MapPath(BatchFilePath);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文