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,178
  • 7
  • 59
  • 70
  • 3
    This has been asked many times before on SO- try using the search. Here's one I found- http://stackoverflow.com/questions/864484/getting-the-path-of-the-current-assembly – RichardOD Sep 22 '09 at 15:15
  • 1
    Also: http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in It's not as simple as ExecutablePath since some assemblies are loaded Click-Once – Blue Toque Sep 22 '09 at 15:20
  • 3
    I did search and did not find anything. It's obvious now, but I was searching for .EXE not assembly. @RichardOD Try not being a jerk. – aphoria Sep 22 '09 at 16:55
  • 1
    BOO. Not really an exact duplicate. I didn't find those because I was searching for .EXE not assembly. – aphoria Sep 24 '09 at 19:39
  • 3
    Furthermore, why would you answer this question and then vote to close it (Steven Sudit, Rowland Shaw)? – aphoria Sep 25 '09 at 11:23
  • 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
  • 558
  • 1
  • 5
  • 18
RRUZ
  • 133,023
  • 16
  • 348
  • 476
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,757
  • 14
  • 23
  • 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,045
  • 1
  • 48
  • 52
0

You should find it in the property: Application.ExecutablePath

Rowland Shaw
  • 37,138
  • 14
  • 95
  • 164
  • I didn't downvote you, but you might notice that RRUZ provided th is answer before you did. – Steven Sudit Sep 22 '09 at 15:20
  • 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 Sep 22 '09 at 16:25
  • Fine, I'll upvote you to neutrality. – Steven Sudit Sep 22 '09 at 16:46
  • And it garners another random down vote? – Rowland Shaw Mar 29 '11 at 08:01
  • Very odd. There's nothing actually wrong with your answer. – Steven Sudit Apr 01 '11 at 19:30
  • I'm fine with upvotes cast to offset downvotes. However, I once did that and then made the mistake of admitting it in the comments. Wow, did I take a beating over that. Apparently it was ok that I upvoted, but it really pissed some people off that I publicly admitted that it was to offset a downvote. Some of those people then went on a witchhunt, downvoting nearly every answer I've ever left. – aphoria Sep 21 '12 at 18:29