代码之家  ›  专栏  ›  技术社区  ›  bingles

.NET设置相对路径

  •  0
  • bingles  · 技术社区  · 16 年前

    我正在开发一个应用程序,在这个应用程序中,我有一个相对于应用程序根目录的图像文件夹。我希望能够在属性->设置设计器中指定此相对路径,例如“图像”。我遇到的问题是,当environment.currentdirectory通过openfiledialog更改时,相对路径无法解析到正确的位置。是否有一种方法可以在设置文件中指定一个路径,该路径意味着始终从应用程序目录开始,而不是从当前目录开始?我知道我总是可以动态地将应用程序路径连接到相对路径的前面,但是我希望我的设置属性能够自行解析。

    6 回复  |  直到 13 年前
        1
  •  1
  •   Scott Dorman    16 年前

    据我所知,不存在允许这种路径解析的内置功能。最好的选择是动态地确定执行目录的应用程序,并将图像路径连接到该目录。你不想用 Environment.CurrentDirectory 特别是出于您提到的原因-当前目录可能并不总是适合这种情况。

    找到执行程序集位置的最安全代码是:

    public string ExecutingAssemblyPath()
    {
       Assembly actualAssembly = Assembly.GetEntryAssembly();
       if (this.actualAssembly == null)
       {
          actualAssembly = Assembly.GetCallingAssembly();
       }
       return actualAssembly.Location;
    }
    
        2
  •  1
  •   Harper Shelby damiankolasa    16 年前

    是否正在查找application.executablePath?这应该告诉您应用程序的可执行文件在哪里,删除可执行文件名,然后将路径附加到它。

        3
  •  0
  •   Chris Marasti-Georg Scott Weinstein    16 年前

    2种选择:

    • 使用该设置的代码可以根据当前正在执行的程序集的目录解析该设置。
    • 您可以创建自己的类型,该类型作为相对于正在执行的程序集的字符串进行序列化,并具有针对当前正在执行的程序集的目录解析的完整路径的访问器。

    代码示例:

    string absolutePath = Settings.Default.ImagePath;
    if(!Path.IsPathRooted(absolutePath))
    {
        string root = Assembly.GetEntryAssembly().Location;
        root = Path.GetDirectoryName(root);
        absolutePath = Path.Combine(root, absolutePath);
    }
    

    这段代码的好处在于它允许在您的设置中使用完全限定的路径或相对路径。如果需要路径相对于其他程序集,则可以更改使用的程序集位置。- GetExecutingAssembly() 将为您提供程序集的位置以及正在运行的代码,以及 GetCallingAssembly() 如果你选择第二种,那就好了。

        4
  •  0
  •   Sire    15 年前

    这似乎在WinForms和ASP.NET中都有效(为 配置文件 ):

    new System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile).Directory;
    

    对于Windows和控制台应用程序,最明显的方法是使用:

    Application.StartupPath
    
        5
  •  0
  •   Graviton    15 年前

    I suggest you 使用assembly.codebase,如下所示:

    public static string RealAssemblyFilePath()
    {
       string dllPath=Assembly.GetExecutingAssembly().CodeBase.Substring(8);
       return dllPath;
    }
    

    你可以试试 Application.ExecutablePath . 但您需要参考System.Windows.Forms。如果你想让你的类库远离窗体和用户界面,这可能不是个好主意。

    你可以试试 Assembly.GetExecutingAssembly().Location . 但是,如果在运行应用程序之前执行“卷影复制”(如默认的nunit行为),则此属性将返回卷影复制位置,而不是实际的物理位置。

    最好的方法是实现一个调用assembly对象的codebase属性并切掉字符串中不相关的部分的函数。

        6
  •  0
  •   Eamon Nerbonne    13 年前

    我使用以下两种方法来帮助实现这一点:

    public static IEnumerable<DirectoryInfo> ParentDirs(this DirectoryInfo dir) {
        while (dir != null) {
            yield return dir;
            dir = dir.Parent;
        }
    }
    public static DirectoryInfo FindDataDir(string relpath, Assembly assembly) {
        return new FileInfo((assembly).Location)
            .Directory.ParentDirs()
            .Select(dir => Path.Combine(dir.FullName + @"\", relpath))
            .Where(Directory.Exists)
            .Select(path => new DirectoryInfo(path))
            .FirstOrDefault();
    }
    

    在开发过程中,当不同的构建脚本最终将东西粘在目录中时,查看父目录更容易使用的原因是 bin\x64\Release\NonsensePath\ .