Calling Assembly to get Application Name

coson picture coson · Jan 22, 2009 · Viewed 32.4k times · Source

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?

Answer

mmx picture mmx · Jan 22, 2009

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