简体   繁体   English

获取程序集执行目录的最佳方法是什么

[英]What's the best way to get the directory from which an assembly is executing

For my apps, I store some configuration file in xml along with the assembly(exe), and something other temporary files for proccessing purpose. 对于我的应用程序,我将一些配置文件与程序集(exe)一起存储在xml中,还有一些其他临时文件用于处理目的。

I found some quirk with ".\\\\" and Application.StartupPath . 我发现了一些带有".\\\\"Application.StartupPath怪癖。

I've been using 我一直在用

String configPath = ".\\config.xml";

It works fine until I called OpenFIleDialog to open some files in other folders, the statement above failed. 它工作正常,直到我调用OpenFIleDialog打开其他文件夹中的某些文件,上面的语句失败。 Apparently ".\\" is referring to "CurrentDirectory", which changes every time when we browse to another folder. 显然“。\\”指的是“CurrentDirectory”,每当我们浏览到另一个文件夹时它就会改变。

At some point, I was using 在某些时候,我正在使用

String configPath = Path.Combine(Application.StartupPath + "config.xml");

At some point, when I need to execute this assembly from another folder by using Process.Start() , things start to fall apart. 在某些时候,当我需要使用Process.Start()从另一个文件夹执行此程序集时,事情开始崩溃。 Apparently the working directory is not set properly, and Application.StartupPath is actually referring to working directory instead of the directory of which the assembly is executing, as I've assumed. 显然工作目录没有正确设置, Application.StartupPath实际上是指工作目录,而不是正在执行程序集的目录,正如我所假设的那样。 So I have to resort to using ProcessInfo to setup the working directory to the assembly's directory. 所以我不得不求助于使用ProcessInfo将工作目录设置到程序集的目录中。 I also had problem with this when I was writing VSTO. 当我写VSTO时,我也有这个问题。

So, my question is, what's the best, simplest and most assured way to get the current directory that the assembly is executing, without those quirks(or misunderstanding) that I've just mentioned? 所以,我的问题是,什么是获得程序集正在执行的当前目录的最好,最简单和最有保证的方法,没有我刚刚提到的那些怪癖(或误解)?

EDIT: I meant to get the directory which the assembly reside 编辑:我的意思是获取程序集所在的目录

EDIT: According to MSDN on AppDomain.BaseDirectory , it seems that it can be changes during runtime, which is what I don't want(Just to clarify, not that I don't want to allow changing BaseDirectory, but rather, when I retrieve it without knowing for sure whether it's been changed) 编辑:根据MSDN上AppDomain.BaseDirectory ,它似乎可以运行,这是我不想要的东西(我想弄清楚,不是我不想允许更改BaseDirectory,而是,当在改变我检索它而不确定它是否已被更改)

EDIT: I've notice that a related question was posted much earlier. 编辑:我注意到一个相关的问题早就发布了。 What would cause the current directory of an executing app to change? 什么会导致正在执行的应用程序的当前目录发生变化?

Thanks guys for the answer. 谢谢你的回答。

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location))

Try the implementation below. 尝试下面的实现。 The CodeBase property is the most reliable way to get the location, and then the rest of the code converts it into standard Windows format (instead of a URI). CodeBase属性是获取位置的最可靠方式,然后其余代码将其转换为标准Windows格式(而不是URI)。

static public string AssemblyLoadDirectory
{
    get
    {
        string codeBase = Assembly.GetCallingAssembly().CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(uri.Path);
        return Path.GetDirectoryName(path);
    }
}

Do you want the actual working directory, or the directory containing the assembly? 您想要实际的工作目录,还是包含程序集的目录? It's not entirely clear. 这还不完全清楚。

There's Environment.CurrentDirectory if you want the working directory, or Path.GetDirectoryName(typeof(Foo).Assembly.ManifestModule.FullyQualifiedName) to find the location of an assembly (or at least its manifest module, which will be the same thing in almost all cases). Environment.CurrentDirectory如果你想在工作目录,或Path.GetDirectoryName(typeof(Foo).Assembly.ManifestModule.FullyQualifiedName)找到一个程序集的位置(或者至少其清单模块,这将是同样的事情在几乎所有情况)。

还有,

System.Reflection.Assembly.GetExecutingAssembly().CodeBase

有点短:

AppDomain.Current.BaseDirectory

I'd try something like this from the following link : 我会从以下链接尝试这样的事情:

string path;
path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
MessageBox.Show( path );

How about 怎么样

string currentAssemblyFile = System.Reflection.Assembly.GetExecutingAssembly().Location;

and then figure it out from there... 然后从那里弄明白......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM