简体   繁体   中英

Calling Assembly to get Application Name

I have a console application ( MyProgram.EXE ) that references a Utilities assembly.

In my Utilities assembly, I have code that does:

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)

When I call it from MyProgram.EXE , I receive " AppName is: Utilities.dll "

What I want is " AppName is: MyProgram.EXE "

What am I doing wrong?

Use GetEntryAssembly() instead to get the assembly containing the entry point.

The better way to do it is using System.Environment.CommandLine property instead.

Specifically:

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

By the way, you want to use GetFileName instead of GetDirectoryName

由于您询问的是 VB.NET,因此您可以轻松地从“My”命名空间中提取此信息,如下所示:

My.Application.Info.AssemblyName

我使用:

CallingAppName = System.Reflection.Assembly.GetEntryAssembly.GetName().Name

就我而言,我无权访问 My.Application,可能是因为我在一个全局类中,所以我使用了:

AppName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name

This is supported all over C#/VB environment.

System.IO.Path.GetFileName(Application.ExecutablePath)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM