Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file name of .EXE [duplicate]

Tags:

vb.net

Possible Duplicates:
Getting the path of the current assembly
C#: How do I get the path of the assembly the code is in?

Using VB 2008, how can I get the file name of a running .EXE from inside itself?

EDIT: This is for a console app, so Application.ExecutablePath will not work.

like image 585
aphoria Avatar asked Sep 22 '09 15:09

aphoria


3 Answers

There are a few ways:

Application.ExecutablePath

or

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

or

System.Reflection.Assembly.GetExecutingAssembly().Location
like image 199
RRUZ Avatar answered Nov 17 '22 20:11

RRUZ


This has been answered before.

From anywhere in your code you could be in an assembly that was loaded by the originating EXE. You also may not have a reference to the Application singleton, so using the Assembly class is your best bet.

Safest way is Assembly.GetEntryAssembly().Location gets the location on the filesystem where the Assembly is currently. If it is shadow copied then this is the Shadow-copy location. If it is click-once deployed, then this is a crazy path to a file in the sandbox area.

The original location of the assembly will be at Assembly.GetEntryAssembly().Codebase

like image 25
Blue Toque Avatar answered Nov 17 '22 18:11

Blue Toque


Process.GetCurrentProcess().MainModule

edit

Another way might be to use Environment.GetCommandLineArgs()[0], but I prefer using Process.

like image 1
Steven Sudit Avatar answered Nov 17 '22 20:11

Steven Sudit