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.
There are a few ways:
Application.ExecutablePath
or
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
or
System.Reflection.Assembly.GetExecutingAssembly().Location
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
Process.GetCurrentProcess().MainModule
edit
Another way might be to use Environment.GetCommandLineArgs()[0], but I prefer using Process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With