How can I make a string variable containing path to my win-app executable folder? I know that there's the simple command Application.ExecutablePath
which returns all the path including the .exe name, but I need that path without the .exe name.
-
What language is this?– MarckvdvCommented Jan 3, 2014 at 19:27
-
I'm using c# language– user2962453Commented Jan 3, 2014 at 19:32
Add a comment
|
2 Answers
You want System.IO.Path.GetDirectoryName:
string appPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
@shf301's answer works for Windows Forms apps. The generic way that should work for any executable (*.exe), whether it's a Windows Forms or not is
string fqn = System.Reflection.Assembly.GetEntryAssembly().Location ;
string dir = Path.GetDirectoryName(fqn) ;
Or even easier:
string baseDir = AppDomain.CurrentDomain.BaseDirectory ;
AppDomain.BaseDirectory
returns "the base directory that the assembly resolver uses to probe for assemblies." For ordinary executables, that's the directory containing the entrypoint assembly.