如何获得一个文件夹,从两个不同的项目相对 [英] How to get the relative of a folder from two different projects

查看:132
本文介绍了如何获得一个文件夹,从两个不同的项目相对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个项目,并从这个文件夹加载图像一个共享库:
C:/ MainProject / PROJECT1 /图片/



PROJECT1的文件夹:
C:/ MainProject / PROJECT1 /文件/斌/ 86 /调试(其中有是PROJECT1.EXE)



Project2中的文件夹:C:/ MainProject / Project2的/斌/ 86 /调试 (这里有project2.exe)



当我调用共享库函数来加载图像,我需要获得的图像文件夹中的相对路径,因为我会调用无论从PROJECT1或项目2的功能。 。此外,我会提出我的MainProject中的其他计算机,所以我不能使用绝对路径



从PROJECT1我会做:

  Directory.GetParent(Directory.GetCurrentDirectory())Parent.Parent.Parent.FullName + @\Images。 



从Project2的,我会做:

  Directory.GetParent(Directory.GetCurrentDirectory())Parent.Parent.Parent.FullName + @Project1\Images。 



我如何通过这两个项目的文件夹?


获得的相对路径< DIV CLASS =h2_lin>解决方案

您可能想测试该代码一点点,使其更加坚固,但只要它应该工作只要你不把它传递UNC地址。
它通过路径分成目录名和比较它们从左到右。那么相对路径是建立。

 公共静态字符串GetRelativePath(字符串SOURCEPATH,串TARGETPATH​​)
{
如果抛出新的ArgumentException(路径必须是绝对的,SOURCEPATH)(Path.IsPathRooted(SOURCEPATH)!);
如果抛出新的ArgumentException(路径必须是绝对的,TARGETPATH​​)(Path.IsPathRooted(TARGETPATH​​)!);

的String [] = sourceParts sourcePath.Split(Path.DirectorySeparatorChar);
的String [] = targetParts targetPath.Split(Path.DirectorySeparatorChar);

INT N;
为(N = 0; N< Math.Min(sourceParts.Length,targetParts.Length); N ++)!
{
如果(string.Equals(sourceParts [N],targetParts [N],StringComparison.CurrentCultureIgnoreCase))
{
中断;
}
}

如果(N == 0)抛出新ApplicationException的(文件必须在同一个卷上);
串relativePath =新的字符串(,sourceParts.Length - N'。').Replace(。+ Path.DirectorySeparatorChar。);
如果(N LT = targetParts.Length)
{
relativePath + =的string.join(Path.DirectorySeparatorChar.ToString(),targetParts.Skip(n)的.ToArray());
}

返回string.IsNullOrWhiteSpace(relativePath)? 。 :relativePath;
}



而不是使用Directory.GetCurrentDirectory的,可以考虑使用Path.GetDirectory(Process.GetCurrentProcess ().MainModule.FileName)。你的当前目录可能是从你的exe文件,这将破坏你的代码不同。 MainModule.FileName直接指向你的exe文件的位置。


I have two projects and one shared library that loads images from this folder: "C:/MainProject/Project1/Images/"

Project1's folder: "C:/MainProject/Project1/Files/Bin/x86/Debug" (where there is project1.exe)

Project2's folder: "C:/MainProject/Project2/Bin/x86/Debug" (where there is project2.exe)

When I call the shared library function to load images, I need to obtain the relative path of "Images" folder, because I will call the function from either project1 or project2. Also I will move my MainProject in other computers, so I can't use absolute paths.

From Project1 I would do:

Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName + @"\Images";

From Project2 I would do:

Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName + @"Project1\Images";

How can I obtain the relative path by both projects folders?

解决方案

You probably want to test this code a little and make it more robust, but it should work as long as you don't pass it UNC addresses. It works by splitting the paths into directory names and comparing them left to right. Then a relative path is built.

    public static string GetRelativePath(string sourcePath, string targetPath)
    {
        if (!Path.IsPathRooted(sourcePath)) throw new ArgumentException("Path must be absolute", "sourcePath");
        if (!Path.IsPathRooted(targetPath)) throw new ArgumentException("Path must be absolute", "targetPath");

        string[] sourceParts = sourcePath.Split(Path.DirectorySeparatorChar);
        string[] targetParts = targetPath.Split(Path.DirectorySeparatorChar);

        int n;
        for (n = 0; n < Math.Min(sourceParts.Length, targetParts.Length); n++ )
        {
            if (!string.Equals(sourceParts[n], targetParts[n], StringComparison.CurrentCultureIgnoreCase))
            {
                break;
            }
        }

        if (n == 0) throw new ApplicationException("Files must be on the same volume");
        string relativePath = new string('.', sourceParts.Length - n).Replace(".", ".." + Path.DirectorySeparatorChar);
        if (n <= targetParts.Length)
        {
            relativePath += string.Join(Path.DirectorySeparatorChar.ToString(), targetParts.Skip(n).ToArray());
        }

        return string.IsNullOrWhiteSpace(relativePath) ? "." : relativePath;
    }

Instead of using Directory.GetCurrentDirectory, consider using Path.GetDirectory(Process.GetCurrentProcess().MainModule.FileName). Your current directory might be different from your exe file which will break your code. MainModule.FileName points directly to the location of your exe file.

这篇关于如何获得一个文件夹,从两个不同的项目相对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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