获取两个文件之间的相对路径的最佳/最简单/最快方法?

发布于 2024-07-16 02:03:46 字数 389 浏览 4 评论 0原文

我正在重构一些 C# 代码,其中一部分是重做一些引用,因为我们正在完全重做文件夹结构。 我想做的只是进入 .csproj 或 .sln 文件并修改路径。

然而,一些参考文献具有类似的路径

"../../../../../../ThirdParty/SomeMicrosoftLibrary/bin/Debug/SomeMicrosoftLibrary.dll

,并且由于我们已经移动了所有内容,所以我需要找到新的相对路径。 但我绝对讨厌尝试这样做(弄清楚我需要放入多少斜线和句号),因为它总是感觉像是一些偶然的科学。

是否有一些简单的方法(实用程序、脚本、代码片段)可以说“这是文件 A,这是文件 B,文件 B 相对于文件 A 的相对路径是什么?”

I'm in the midst of some refactoring some C# code and part of this is to redo some references, because we're redoing the folder structure entirely. What I'd like to do is just go into the .csproj or .sln file and modify the paths.

However some of the references have paths like

"../../../../../../ThirdParty/SomeMicrosoftLibrary/bin/Debug/SomeMicrosoftLibrary.dll

And since we've moved everything around I need to find the new relative path. But I absolutely hate trying to do this (figure out how many slashes and periods I need to put in) since it always feels like some hit or miss science.

Is there some easy way (utility, script, snippet of code) to say "here's file A, here's file B, what is the relative path of file B in relation to file A?"

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

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

发布评论

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

评论(3

绝對不後悔。 2024-07-23 02:03:46

对于 Ruby:

$ ruby -e "require 'pathname'; puts Pathname.new('foo/bar').relative_path_from(Pathname.new('baz/x/y/z')).to_s"
../../../../foo/bar

我很确定 Python 有类似的方法,尽管我手头没有。

With Ruby:

$ ruby -e "require 'pathname'; puts Pathname.new('foo/bar').relative_path_from(Pathname.new('baz/x/y/z')).to_s"
../../../../foo/bar

I'm pretty sure Python has a similar method, though I don't have it handy.

套路撩心 2024-07-23 02:03:46

生成相对路径

它是 Java 语言,但可以轻松转换为 C#。

Generate a Relative Path

It's in Java, but easily translatable to C#.

豆芽 2024-07-23 02:03:46

我知道这已经过时了,但我一直在寻找这个问题的答案,所以这里有一个方法可以做到这一点,以防万一它可以帮助其他人在未来

/// <summary>
/// Finds the relative path of B with respect to A
/// </summary>
/// <param name="A">The path you're navigating from</param>
/// <param name="B">The path you're navigating to</param>
/// <returns></returns>
static string Get_PathB_wrt_PathA(string A, string B)
{
    string result = "";

    if (A == B)
        return result;

    var s = new char[] { '\\' };
    var subA = A.Split(s, StringSplitOptions.RemoveEmptyEntries).ToList();
    var subB = B.Split(s, StringSplitOptions.RemoveEmptyEntries).ToList();

    int L = subA.Count >= subB.Count ? subB.Count : subA.Count;
    int i = 0;

    for (i = 0; i < L; i++)
    {
        if (subA[0] == subB[0])
        {
            subA.RemoveAt(0);
            subB.RemoveAt(0);
        }
        else
            break;
    }

    for (i = 0; i <= subA.Count - 1; i++)
    {
        result += @"..\"; //this navigates to the preceding directory
    }

    for (i = 0; i < subB.Count; i++)
    {
        result += subB[i] + "\\";
    }

    result = result.Substring(0, result.Length - 1); //remove the extra backslash
    return result;
}

I know this is old but I was looking for the answer to this so here's a method which does it, just in case it might help anyone else out in the future

/// <summary>
/// Finds the relative path of B with respect to A
/// </summary>
/// <param name="A">The path you're navigating from</param>
/// <param name="B">The path you're navigating to</param>
/// <returns></returns>
static string Get_PathB_wrt_PathA(string A, string B)
{
    string result = "";

    if (A == B)
        return result;

    var s = new char[] { '\\' };
    var subA = A.Split(s, StringSplitOptions.RemoveEmptyEntries).ToList();
    var subB = B.Split(s, StringSplitOptions.RemoveEmptyEntries).ToList();

    int L = subA.Count >= subB.Count ? subB.Count : subA.Count;
    int i = 0;

    for (i = 0; i < L; i++)
    {
        if (subA[0] == subB[0])
        {
            subA.RemoveAt(0);
            subB.RemoveAt(0);
        }
        else
            break;
    }

    for (i = 0; i <= subA.Count - 1; i++)
    {
        result += @"..\"; //this navigates to the preceding directory
    }

    for (i = 0; i < subB.Count; i++)
    {
        result += subB[i] + "\\";
    }

    result = result.Substring(0, result.Length - 1); //remove the extra backslash
    return result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文