27

I have two applications, e.g. App1 and App2. When running normally, App1 will show its assembly executable location. But when I call App1 from App2, it returns App2's startup location.

So, how do I get App1's startup path when I call App1 from App2?

janw
  • 8,758
  • 11
  • 40
  • 62
TrungNV
  • 269
  • 1
  • 4
  • 7
  • 2
    Welcome to StackOverflow! Please show what you've tried and clarify what exactly you want: "startup path" or "assembly location"? – René Vogt Jun 28 '16 at 09:04
  • i get start up path = Application.StartupPath. When i run App1 nomal up path return true location. But when i call App1 from App2, it return App2 location. So how to get App1 start up path when i call App1 from App2? – TrungNV Jun 28 '16 at 09:15
  • So of course it is the same. the StartupPath is not the path of the assembly, but the working directory from which the app got started. So if your App2 does not change it's working directory, App1 has the same startuppath. – René Vogt Jun 28 '16 at 09:17

1 Answers1

57

You can get the directory of the currently executing assembly with this:

string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

GetExecutingAssembly() returns the currently executing assembly and Location is the full path or UNC path of that assembly.

Path.GetDirectoryName() returns the directory of a full path.


Note that the assembly's path is not the same as the startup path. The startup path is the working directory from which you started an application. And if your app does not change it's working directory, all apps started by the first app will have the same startup path.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • 2
    Can not get by string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); it return App2 startup path. – TrungNV Jun 28 '16 at 09:25
  • 1
    fixed a bug today, `Application.Startup` was navigating in to Internet Explorer directory and of course we cannot write files there, this fixed it to write in assembly's directory, Thanks! – Ehsan Sajjad Mar 14 '19 at 13:45