调用程序集获取应用程序名称VB.NET

6 .net vb.net

我有一个MyProgram.EXE引用Utilities程序集的控制台应用程序().

在我的Utilities程序集中,我有以下代码:

Dim asm As Assembly = Assembly.GetExecutingAssembly()
Dim location As String = asm.Location
Dim appName As String = System.IO.Path.GetDirectoryName(location)
Conole.WriteLine("AppName is: {0}", appName)
Run Code Online (Sandbox Code Playgroud)

当我打电话给它时MyProgram.EXE,我会收到" AppName is: Utilities.dll"

我想要的是" AppName is: MyProgram.EXE"

我究竟做错了什么?

Meh*_*ari 11

GetEntryAssembly()而是使用来获取包含入口点的程序集.

更好的方法是使用System.Environment.CommandLine属性.

特别:

Dim location As String = System.Environment.GetCommandLineArgs()(0)
Dim appName As String = System.IO.Path.GetFileName(location)
Conole.WriteLine("AppName is: {0}", appName)
Run Code Online (Sandbox Code Playgroud)

顺便说一句,你想用GetFileName而不是GetDirectoryName

  • 另外(根据http://stackoverflow.com/questions/980202/how-do-i-find-the-current-executable-filename):"如果在调试模式下运行WCF服务应用程序,GetEntryAssembly方法也返回null在IIS下.诚然,这是一种只有开发人员会遇到的罕见情况. - Gravitas 2010年11月10日10:39" (2认同)

atc*_*way 10

既然是你想要的VB.NET,你可以从'My'命名空间轻松提取这些信息,如下所示:

My.Application.Info.AssemblyName
Run Code Online (Sandbox Code Playgroud)