tags:

views:

1408

answers:

4

I have two exe files in the same folder, I can run exe2 from a button in exe1. Today I was observing a customer over a remote (terminal services) session and exe2 failed to run 'File not found' error, yet exe1 was in the same directort when we checked. So should I be using AppDomain.CurrentDomain.BaseDirectory or System.Environment.CurrentDirectory?

Thanks

+3  A: 

As I understand it, you should use BaseDirectory. CurrentDirectory could change over the course of the program's execution.

Joel Coehoorn
+9  A: 

If you want to find files in the same directory as your application, AppDomainCurrentDomain.BaseDirectory is the correct choice.

Environment.CurrentDirectory is a value that can and will change throught the course of running your application. For instance, using default parameters, the OpenFileDialog in WinForms will change this value to the directory where the file was selected from.

JaredPar
I wish I could vote you up more than once on this one.
David Stratton
A: 

I usually use something like:

            string AppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            AppPath = AppPath.Replace("file:\\", "");
Zahymaka
+3  A: 

AppDomain.CurrentDomain.BaseDirectory returns the directory from where the current application domain was loaded. System.Environment.CurrentDirectory returns the current system directory. In your case AppDomain.CurrentDomain.BaseDirectory is the best solution.

Albert