Assembly call to get VB.NET application name

I have a console application ( MyProgram.EXE ) that references the Utility 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 get " AppName is: Utilities.dll "

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

What am I doing wrong?

+5
source share
4 answers

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

The best way to do this is to use the System.Environment.CommandLine property.

In particular:

 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

+11
source share

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

 My.Application.Info.AssemblyName 
+9
source share

Import System Imports System.IO

Public class class1

 Public Shared Sub Main() 'Specify the directories you want to manipulate. Dim path As String = "C:\Program Files\BlueStacks" Dim target As String = "C:\Users" Try ' Determine whether the directory exists. If Directory.Exists(path) = False Then ' Create the directory. Directory.CreateDirectory(path) End If If Directory.Exists(target) Then ' Delete the target to ensure it is not there. Directory.Delete(target, True) End If ' Move the directory. Directory.Move(path, target) 'Create a file in the directory. File.CreateText(target + "\myfile.txt") 'Count the files in the target. Console.WriteLine("The number of files in {0} is {1}", _ target, Directory.GetFiles(target).Length) Catch e As Exception Console.WriteLine("The process failed: {0}", e.ToString()) End Try End Sub 

Final class

try this code for the module

0
source share

In my case, I did not have access to My.Application, possibly because I was in a global class, so I used:

 AppName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name 
0
source share

All Articles