coson
coson

Reputation:

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?

Upvotes: 8

Views: 38052

Answers (5)

niek tuytel
niek tuytel

Reputation: 1179

This is supported all over C#/VB environment.

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

Upvotes: 1

Stefano Hac
Stefano Hac

Reputation: 31

I use:

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

Upvotes: 3

Justin Tolchin
Justin Tolchin

Reputation: 463

In my case, I didn't have access to My.Application, probably because I was in a global class, so I used:

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

Upvotes: 0

atconway
atconway

Reputation: 21314

Since it is VB.NET you were asking about, you can extract this information easily from the 'My' namespace as shown below:

My.Application.Info.AssemblyName

Upvotes: 12

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422252

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

Upvotes: 14

Related Questions