使用 Assembly 与 AppDomain 查找我的主要可执行文件的路径
我是一名 .NET 用户,我的目标很简单,就是找到主执行程序集(EXE 文件)目录的绝对路径。
我有几个候选:
Assembly.GetExecutingAssembly().CodeBase
Assembly.GetExecutingAssembly().Location
AppDomain.CurrentDomain.BaseDirectory
如果通过判断.NET 文档 - 我倾向于 CodeBase
。 任何人都可以用比 .NET 文档更具体的术语来阐明这三个方面吗?也许可以举个例子来证明差异?
I'm a .NET user, and my goal is as simple as finding the absolute path of the directory of my main executing assembly (the EXE file).
I have several candidates:
Assembly.GetExecutingAssembly().CodeBase
Assembly.GetExecutingAssembly().Location
AppDomain.CurrentDomain.BaseDirectory
If to judge by the .NET documentation - I'm leaning towards CodeBase
.
Can anyone shed light over all three in a bit more specific terms than the .NET documentation? An example to demonstrate the difference perhaps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定
AppDomain.CurrentDomain.BaseDirectory
,但Assembly.GetExecutingAssembly().CodeBase
和Assembly.GetExecutingAssembly().Location
之间的区别 在此博文中解释了 code>。因此,如果您需要执行文件的真实目录,那么您最好的选择是
Location
。I'm not sure about
AppDomain.CurrentDomain.BaseDirectory
, but the difference betweenAssembly.GetExecutingAssembly().CodeBase
andAssembly.GetExecutingAssembly().Location
was explained in this blog post.So it seems your best bet is
Location
if you need the real directory the file was executed from.我会使用
GetEntryAssembly()
而不是GetExecutingAssembly()
。要了解原因,请执行以下操作:
ClassLibrary1
) 添加到解决方案并从控制台项目引用它。将其放入
ClassLibrary1
中:将其放入控制台的
Program.cs
中:构建解决方案,将
ClassLibrary1.dll
复制到c:\temp并运行。
正如您将看到的,
GetExecutingAssembly()
在某些情况下可能会欺骗您。最后一点,如果您的应用程序是 Windows 窗体应用程序,则可以仅使用
Application.ExecutablePath
。I would use
GetEntryAssembly()
instead ofGetExecutingAssembly()
.To see why, do this:
ClassLibrary1
) to the solution and reference it from the Console Project.Put this in
ClassLibrary1
:Put this in console's
Program.cs
:Build the solution, copy
ClassLibrary1.dll
toc:\temp
and run.As you will see,
GetExecutingAssembly()
may trick you in certain conditions.One last note, if your app is a Windows Forms one, you can just use
Application.ExecutablePath
.不幸的是,如果您使用像 XenoCode postbuild 这样的虚拟化,上述所有方法都可能失败。
我测试了很多方法,并在此处找到了另一种解决方案。
我发现只有
返回可执行文件的正确文件名。
因此,将文件名与
Assembly.GetEntryAssembly().Location
中的路径相结合,您将获得可执行文件的正确路径。Unfortunately, all the methods above can fail if you use a virtualization like XenoCode postbuild.
I have tested many methods and found another solution here.
I've found that only the
returns the correct filename of the executable.
So combining the filename with the path from
Assembly.GetEntryAssembly().Location
you will get the correct path of the executable.来自: http://msdn.microsoft.com/ en-us/library/system.reflection.assemble.codebase.aspx
Assembly.CodeBase
对于AppDomain.CurrentDomain.BaseDirectory,老实说,从实际角度来看,我不知道其中的差异。
From: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.codebase.aspx
Assembly.CodeBase
For
AppDomain.CurrentDomain.BaseDirectory
, I honestly have no idea about the differences from a practical point of view.使用系统.Linq
Use System.Linq