The best method is probably this:
string exeFilePath = System.Reflection.Assembly.GetEntryAssembly().Location;
string exeDirectory = System.IO.Path.GetDirectoryName(exeFilePath);
If founds the location of the main executable model of the entry assembly of the application (where you entry point is, usually the method
Main
), regardless of the assembly where this code is called.
Note, there are many solutions of this problems, but many of them are not so good. For example,
Application.StartupPath
requires
System.Windows.Forms
, other methods depend on how application is hosted (for example, may not work correctly for a Windows Service application or under debugger), but the method I've demonstrated always work correctly.
—SA