0

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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
user2962453
  • 143
  • 2
  • 10

2 Answers2

3

You want System.IO.Path.GetDirectoryName:

string appPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
shf301
  • 31,086
  • 2
  • 52
  • 86
0

@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.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135