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

时间:2023-01-14 11:17:32

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. Apparently ".\" is referring to "CurrentDirectory", which changes every time when we browse to another folder.

它工作正常,直到我调用OpenFIleDialog打开其他文件夹中的某些文件,上面的语句失败。显然“。\”指的是“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. 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. So I have to resort to using ProcessInfo to setup the working directory to the assembly's directory. I also had problem with this when I was writing VSTO.

在某些时候,当我需要使用Process.Start()从另一个文件夹执行此程序集时,事情开始崩溃。显然工作目录设置不正确,Application.StartupPath实际上是指工作目录,而不是正在执行程序集的目录,正如我所假设的那样。所以我不得不求助于使用ProcessInfo将工作目录设置到程序集的目录中。当我写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)

编辑:根据AppDNmain.BaseDirectory上的MSDN,它似乎可以在运行时更改,这是我不想要的(只是澄清,不是我不想允许更改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.

谢谢你的回答。

7 个解决方案

#1


43  

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

#2


11  

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);
    }
}

#3


10  

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)来查找程序集的位置(或者至少它的清单模块,这几乎是相同的东西)所有情况)。

#4


5  

There is also,

还有,

System.Reflection.Assembly.GetExecutingAssembly().CodeBase

#5


4  

A tad shorter:

有点短:

AppDomain.Current.BaseDirectory

#6


2  

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 );

#7


1  

How about

怎么样

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

and then figure it out from there...

然后从那里弄清楚......

#1


43  

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

#2


11  

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);
    }
}

#3


10  

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)来查找程序集的位置(或者至少它的清单模块,这几乎是相同的东西)所有情况)。

#4


5  

There is also,

还有,

System.Reflection.Assembly.GetExecutingAssembly().CodeBase

#5


4  

A tad shorter:

有点短:

AppDomain.Current.BaseDirectory

#6


2  

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 );

#7


1  

How about

怎么样

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

and then figure it out from there...

然后从那里弄清楚......