C# .NET - How to get the path of an Executable file using C#
Asked By suresh vsvb on 14-May-09 03:08 AM
Hi
I am having a Executable(Set.exe) and a DLL(some.dll). I am starting the application set.exe and which intern will calls the DLL(some.dll). I want to get the path of Set.exe from which it is started.
The condition is that i will write the command/call for getting the path of this EXE(Set.exe) in the DLL.So i want to get the path of EXE in the DLL and i will use this in the DLL later.
I am tried the methods getexecutingassembly and getentryassembly etc. But none of them worked for me.
Thanks for your ideas...
put the exe in your bin folder - Rao B replied to suresh vsvb on 14-May-09 03:16 AM
then try with getexecutingassembly
I cant put this in Bin. - suresh vsvb replied to Rao B on 14-May-09 03:22 AM
I am doing this as part of installation. So i cant put that in BIN.
Ravenet Rasaiyah replied to suresh vsvb on 14-May-09 03:48 AM
Hi
To Get executable start exe application path.
For Windows Forms Applications
string startpointPath = Application.ExecutablePath;
For Console level Applications.
string rootPath=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
For the dll also you can use the last code .
thank you
Getting only DLL path But not the Executable path - suresh vsvb replied to Ravenet Rasaiyah on 14-May-09 04:49 AM
string rootPath=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
This one returning the DLL path but not the EXE path.
Ravenet Rasaiyah replied to suresh vsvb on 14-May-09 04:58 AM
Hi
You need access Entry assembly to get path.
string rootPath=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
thank you
What is the class i should use - Asked By suresh vsvb on 14-May-09 05:11 AM
It seems i am not clear in using the Myclass. What is the Class i should use.
Do i have to use any Class that is defiend and used in the projects for Set.exe?
I already used this call - suresh vsvb replied to Ravenet Rasaiyah on 14-May-09 05:16 AM
I already used this one and it is not returning any path Neither DLL nor EXE.
I have mentioned this at the start of the post. Is the path from the EXE which we want is to be alive(needs to be running currently) i mean process?
Once can i recheck my question and let me know if u have any doubts in my requirement.
Thanks !
try using the ExecutablePath() or StartupPath() methods - H K replied to suresh vsvb on 14-May-09 04:23 PM
Try using the ExecutablePath() or StartupPath() methods to get the path of the current executing assembly.
This method is available in the Application class.
String aPath = Application.ExecutablePath();
for more info refer
http://www.thescarms.com/dotNet/apppath.aspx
Tommy replied to H K on 04-Sep-10 01:45 AM
sorry for the resurrection of this post :)
The property System.Windows.Forms.Application.ExecutablePath is exactly what you want.
I don't know why it's in the Windows.Forms namespace but ... works.
Alternatively I used System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName