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.

1
  • Not a duplicate. I think he's finding the path and the filename only, which is System.IO.GetFileName(System.Reflection.Assembly.GetExecutingAssembly.Location) Commented May 16, 2019 at 7:02

4 Answers 4

23

There are a few ways:

Application.ExecutablePath

or

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

or

System.Reflection.Assembly.GetExecutingAssembly().Location
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, but Application is in System.Windows.Forms. Is this a WinForm app?
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.
Actually, the last one isn't quite correct and will produce a path to a .DLL file if that particular assembly is, for example, a sub-project that creates a Class Library as a sub-part of a larger project that creates an Executable. Instead use System.Reflection.Assembly.GetEntryAssembly().Location to get path of the actual containing EXE.
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

2 Comments

In the case of click-once deployment, will the Process method return a useful result?
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.
1

Process.GetCurrentProcess().MainModule

edit

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

4 Comments

Steve must be Process.GetCurrentProcess().MainModule.FileName
That's not entirely correct: you have a choice between getting the FileName or the ModuleName.
ModuleName Gets the main module for the associated process. not the filename.
Depending on what you need, that may be more useful.
0

You should find it in the property: Application.ExecutablePath

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.