Environment.CurrentDirectory is yielding unexpected results when running installed app Environment.CurrentDirectory is yielding unexpected results when running installed app wpf wpf

Environment.CurrentDirectory is yielding unexpected results when running installed app


If you want to get the path to the directory under which your executable runs, you should not rely on the Environment.CurrentDirectory, since it can be changed in a number of ways (shotrtcut settings, etc). Try one of these options instead:

System.IO.Path.GetDirectoryName(Application.ExecutablePath);

or

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);


Use Application.StartupPath instead of Environment.CurrentDirectory.
I've had a similar problem, where the CurrentDirectory was being changed inadvertently by something like an OpenFileDialog without me even realizing it.
In your case, it sounds like the process that you're starting the application form is changing the CurrentDirectory unbeknownst to you.


Since you said that your application is using WPF, you can use the code below instead of Application.StartupPath :

String appPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);