In VB.NET 2012, the fastest and most direct way to get the assembly name (e.g., myfile.exe) is by using the System.Reflection.Assembly class, specifically the GetEntryAssembly or GetExecutingAssembly method, depending on your use case.
✅ To get the name of the currently running executable (e.g., myfile.exe):
Dim exeName As String = System.Reflection.Assembly.GetEntryAssembly().Location
Or if you just want the file name (without the full path):
Dim exeNameOnly As String = System.IO.Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location)
This will give you "myfile.exe".
🔍 Explanation:
-
GetEntryAssembly():
- Gets the executable that started the application.
- Most appropriate for getting the main
.exe.
-
GetExecutingAssembly():
- Gets the assembly that the code is currently running in.
- Useful if you're inside a DLL and want to know which DLL it is.
-
.Location:
- Returns the full path to the assembly (e.g.,
C:\path\to\myfile.exe).
-
Path.GetFileName(...):
- Extracts just the file name from the full path.
🚀 Fast alternative (if used in the main EXE itself):
Dim appName As String = My.Application.Info.AssemblyName
This is also very fast and clean within the main project, especially in Windows Forms applications. It returns the assembly name without the extension by default. You can append .exe if needed.
If you need the full file name including .exe, combine it like this:
Dim exeName As String = My.Application.Info.AssemblyName & ".exe"
But note: This only works in projects where the My namespace is available, such as Windows Forms or Console apps with proper settings.
Summary
| Method | Description |
|---|
GetEntryAssembly().Location | Full path to the EXE (best for standalone EXEs) |
GetFileName(...) | Just the file name (myfile.exe) |
My.Application.Info.AssemblyName & ".exe" | Quick access in WinForms/Console apps |
Let me know if you're working in a specific context like a DLL or service!