.net 获取程序集正在执行的目录的最佳方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/253468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the best way to get the directory from which an assembly is executing
提问by faulty
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 OpenFIleDialogto 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.StartupPathis 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)
编辑:根据AppDomain.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.
谢谢大家的回答。
回答by korro
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location))
回答by John Sibly
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);
}
}
回答by Jon Skeet
Do you want the actual working directory, or the directory containing the assembly? It's not entirely clear.
您想要实际的工作目录,还是包含程序集的目录?这并不完全清楚。
There's Environment.CurrentDirectoryif 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)找到一个组件(或者至少其清单模块,这将是在几乎所有情况下,同样的事情)的位置。
回答by tafa
There is also,
还有,
System.Reflection.Assembly.GetExecutingAssembly().CodeBase
回答by rslite
A tad shorter:
短一点:
AppDomain.Current.BaseDirectory
回答by Mat Nadrofsky
回答by GalacticCowboy
How about
怎么样
string currentAssemblyFile = System.Reflection.Assembly.GetExecutingAssembly().Location;
and then figure it out from there...
然后从那里弄清楚......

