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
  • 58,479
  • 27
  • 141
  • 176
user2962453
  • 143
  • 2
  • 10

2 Answers2

3

You want System.IO.Path.GetDirectoryName:

string appPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
Michael Petrotta
  • 58,479
  • 27
  • 141
  • 176
shf301
  • 30,640
  • 2
  • 50
  • 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
  • 65,549
  • 13
  • 92
  • 133