tags:

views:

692

answers:

4

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.

+4  A: 

try this

Application.ExecutablePath

or

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

or

System.Reflection.Assembly.GetExecutingAssembly().Location

Bye.

RRUZ
Ok, but Application is in System.Windows.Forms. Is this a WinForm app?
Steven Sudit
Thank you so much for adding my answer.
Steven Sudit
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
Bye!!
jeffamaphone
+1  A: 

Process.GetCurrentProcess().MainModule

edit

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

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

You should find it in the property: Application.ExecutablePath

Rowland Shaw
I didn't downvote you, but you might notice that RRUZ provided th is answer before you did.
Steven Sudit
Ah, But I'd included a link to the documentation, which is why I kept my answer up (and also why he beat me by a few seconds)
Rowland Shaw
Fine, I'll upvote you to neutrality.
Steven Sudit
+3  A: 

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

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