如何从绝对路径获取相对路径 [英] How to get relative path from absolute path

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

问题描述

我的应用程序中有一部分显示用户通过 OpenFileDialog 加载的文件路径.显示整个路径占用了太多空间,但我不想只显示文件名,因为它可能不明确.所以我更愿意显示相对于 assembly/exe 目录的文件路径.

There's a part in my apps that displays the file path loaded by the user through OpenFileDialog. It's taking up too much space to display the whole path, but I don't want to display only the filename as it might be ambiguous. So I would prefer to show the file path relative to the assembly/exe directory.

例如,程序集位于 C:Program FilesDummy FolderMyProgram,文件位于 C:Program FilesDummy FolderMyProgramDatadatafile1.dat 然后我希望它显示 .Datadatafile1.dat.如果文件位于 C:Program FilesDummy Folderdatafile1.dat,那么我想要 ..datafile1.dat.但如果文件在根目录或根目录下的 1 个目录,则显示完整路径.

For example, the assembly resides at C:Program FilesDummy FolderMyProgram and the file at C:Program FilesDummy FolderMyProgramDatadatafile1.dat then I would like it to show .Datadatafile1.dat. If the file is in C:Program FilesDummy Folderdatafile1.dat, then I would want ..datafile1.dat. But if the file is at the root directory or 1 directory below root, then display the full path.

您会推荐什么解决方案?正则表达式?

What solution would you recommend? Regex?

基本上我想在不占用太多屏幕空间的情况下显示有用的文件路径信息.

Basically I want to display useful file path info without taking too much screen space.

只是为了澄清一点.此解决方案的目的是帮助用户或我自己了解我上次加载了哪个文件以及它大致来自哪个目录.我正在使用只读文本框来显示路径.大多数时候,文件路径比文本框的显示空间要长得多.路径应该提供信息,但不够重要,无法占用更多屏幕空间.

Just to clarify a little bit more. The purpose of this solution is to help user or myself knowing which file did I loaded last and roughly from which directory was it from. I'm using a readonly textbox to display the path. Most of the time, the file path is much longer than the display space of the textbox. The path is supposed to be informative but not important enough as to take up more screen space.

Alex Brault 的评论很好,Jonathan Leffler 也很好.DavidK 提供的 Win32 函数只能帮助解决部分问题,而不是全部问题,但还是感谢.至于 James Newton-King 的解决方案,等我有空的时候再试一下.

Alex Brault comment was good, so is Jonathan Leffler. The Win32 function provided by DavidK only help with part of the problem, not the whole of it, but thanks anyway. As for James Newton-King solution, I'll give it a try later when I'm free.

推荐答案

.NET Core 2.0 有 Path.GetRelativePath,否则就用这个.

.NET Core 2.0 has Path.GetRelativePath, else, use this.

/// <summary>
/// Creates a relative path from one file or folder to another.
/// </summary>
/// <param name="fromPath">Contains the directory that defines the start of the relative path.</param>
/// <param name="toPath">Contains the path that defines the endpoint of the relative path.</param>
/// <returns>The relative path from the start directory to the end path or <c>toPath</c> if the paths are not related.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="UriFormatException"></exception>
/// <exception cref="InvalidOperationException"></exception>
public static String MakeRelativePath(String fromPath, String toPath)
{
    if (String.IsNullOrEmpty(fromPath)) throw new ArgumentNullException("fromPath");
    if (String.IsNullOrEmpty(toPath))   throw new ArgumentNullException("toPath");

    Uri fromUri = new Uri(fromPath);
    Uri toUri = new Uri(toPath);

    if (fromUri.Scheme != toUri.Scheme) { return toPath; } // path can't be made relative.

    Uri relativeUri = fromUri.MakeRelativeUri(toUri);
    String relativePath = Uri.UnescapeDataString(relativeUri.ToString());

    if (toUri.Scheme.Equals("file", StringComparison.InvariantCultureIgnoreCase))
    {
        relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
    }

    return relativePath;
}

这篇关于如何从绝对路径获取相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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