如何获取.EXE的文件名

时间:2009-09-22 15:09:18

标签: vb.net

  

可能重复:
  Getting the path of the current assembly
  C#: How do I get the path of the assembly the code is in?

使用VB 2008,如何从内部获取正在运行的.EXE的文件名?

编辑:这适用于控制台应用,因此Application.ExecutablePath无效。

4 个答案:

答案 0 :(得分:18)

有几种方法:

Application.ExecutablePath

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

System.Reflection.Assembly.GetExecutingAssembly().Location

答案 1 :(得分:4)

已经回答before

您可以在代码中的任何位置使用由原始EXE加载的程序集。您也可能没有对Application单例的引用,因此使用Assembly类是最好的选择。

最安全的方式是Assembly.GetEntryAssembly()。位置获取当前程序集所在文件系统的位置。如果是shadow copied,则这是影子复制位置。如果它是click-once部署的,那么这是sandbox区域中文件的疯狂路径。

装配的原始位置将位于Assembly.GetEntryAssembly().Codebase

答案 2 :(得分:1)

Process.GetCurrentProcess()。MainModule

修改

另一种方法可能是使用Environment.GetCommandLineArgs()[0],但我更喜欢使用Process。

答案 3 :(得分:0)

您应该在酒店中找到它:Application.ExecutablePath

相关问题