12

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.

Community
  • 1
  • 1
aphoria
  • 19,796
  • 7
  • 64
  • 73
  • Not a duplicate. I think he's finding the path and the filename only, which is `System.IO.GetFileName(System.Reflection.Assembly.GetExecutingAssembly.Location)` – amegyoushi May 16 '19 at 07:02

4 Answers4

21

There are a few ways:

Application.ExecutablePath

or

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

or

System.Reflection.Assembly.GetExecutingAssembly().Location
Dog Lover
  • 618
  • 1
  • 6
  • 18
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 1
    Ok, but Application is in System.Windows.Forms. Is this a WinForm app? – Steven Sudit Sep 22 '09 at 15:13
  • RRUZ, I think Oplopanax has a good point about using GetEntryAssembly instead of GetExecutingAssembly. Likewise, CodeBase is a better choice than Location, since it doesn't also include the path. – Steven Sudit Sep 22 '09 at 16:04
4

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

Community
  • 1
  • 1
Blue Toque
  • 1,775
  • 13
  • 24
  • In the case of click-once deployment, will the Process method return a useful result? – Steven Sudit Sep 22 '09 at 15:31
  • 1
    I am not entirely certain, but I think the Process method will get the name of the clickonce launcher process in much the same way as for a web app it would get the IIS worker process. – Blue Toque Sep 22 '09 at 17:58
1

Process.GetCurrentProcess().MainModule

edit

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

Steven Sudit
  • 19,391
  • 1
  • 51
  • 53
0

You should find it in the property: Application.ExecutablePath

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166