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

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

问题描述

有一个在我的应用程序,显示通过打开文件对话框,用户加载的文件路径的一部分。它占用了太多的空间来显示整个路径,但我不希望只显示文件名,因为它可能是不明确的。因此,我将preFER显示相对于组装/ exe文件目录中的文件路径。

例如,装配在位于C:\ Program Files文件\虚拟文件夹\ MyProgram,并在文件的C:\ Program Files文件\虚拟文件夹\ MyProgram \ DATA \ datafile1.dat那么我想它秀。\ DATA \ datafile1.dat。如果该文件是在C:\ Program Files文件\虚拟文件夹\ datafile1.dat,那么我想.. \ datafile1.dat。但如果该文件是在根目录或1目录下根,然后显示完整路径。

你会推荐什么样的解决方案呢?正则表达式?

基本上,我想没有采取太多的屏幕空间来显示有用的文件路径信息。

编辑:只是为了澄清一点点。这种解决方案的目的是帮助用户还是我自己知道哪些文件,我做了最后的,大致从从目录是它装载。我使用的是只读的文本框,显示的路径。大部分时间,文件路径比文本框的显示空间中长得多。该路径被假设是信息,但没有足够重要,因为要占用更多的屏幕空间。

亚历克斯Brault评论是好,所以是乔纳森·莱弗勒。通过DavidK提供的Win32函数只能帮助的问题,而不是整个它的一部分,但无论如何感谢。至于詹姆斯·牛顿 - 国王的解决方案,我会当我是自由后试试看。

解决方案

  ///<总结>
///创建从一个文件或文件夹到另一个相对路径。
///< /总结>
///&所述; PARAM NAME =fromPath>包含定义的相对路径的开始的目录&所述; /参数>
///< PARAM NAME =toPath>包含定义的相对路径的终点的路径< /参数>
///<返回>从一开始的目录来结束路径或LT的相对路径; c取代; toPath< / c取代;如果路径是不相关的< /回报>
///<异常CREF =ArgumentNullException>< /异常>
///<异常CREF =UriFormatException>< /异常>
///<异常CREF =InvalidOperationException异常>< /异常>
公共静态字符串MakeRelativePath(字符串fromPath,字符串toPath)
{
    如果(String.IsNullOrEmpty(fromPath))抛出新ArgumentNullException(fromPath);
    如果(String.IsNullOrEmpty(toPath))抛出新ArgumentNullException(toPath);

    乌里fromUri =新的URI(fromPath);
    乌里toUri =新的URI(toPath);

    如果(!fromUri.Scheme = toUri.Scheme){返回toPath; } //路径不能进行相对的。

    乌里relativeUri = fromUri.MakeRelativeUri(toUri);
    串relativePath = Uri.UnescapeDataString(relativeUri.ToString());

    如果(toUri.Scheme.ToUpperInvariant()==文件)
    {
        relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar,Path.DirectorySeparatorChar);
    }

    返回relativePath;
}
 

There's a part in my apps that display 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.

For example, the assembly resides at "C:\Program Files\Dummy Folder\MyProgram" and the file at "C:\Program Files\Dummy Folder\MyProgram\Data\datafile1.dat" then I would like it to show ".\Data\datafile1.dat". If the file is at "C:\Program Files\Dummy Folder\datafile1.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 wanted to display useful file path info without taking too much screen space.

EDIT: 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 suppose to be informative but not important enough as to take up more screen space.

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.

解决方案

/// <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.ToUpperInvariant() == "FILE")
    {
        relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
    }

    return relativePath;
}

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

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