Should I use AppDomain.CurrentDomain.BaseDirectory or System.Environment.CurrentDirectory?

I have two exe files in one folder, I can start exe2 using the button in exe1. Today I watched the client over a remote (terminal services) session, and exe2 could not run the "File not found" error, but exe1 was in the same directory as we checked. So should I use AppDomain.CurrentDomain.BaseDirectory or System.Environment.CurrentDirectory ?

thank

+81
c # exception
Mar 23 '09 at 19:20
source share
7 answers

If you want to find files in the same directory as your application, AppDomain.CurrentDomain.BaseDirectory is the right choice.

Environment.CurrentDirectory is a value that can and will change during the execution of your application. For example, using the default options, OpenFileDialog in WinForms will change this value to the directory in which the file was selected.

+177
Mar 23 '09 at 19:24
source share

AppDomain.CurrentDomain.BaseDirectory returns the directory from which the current application domain was downloaded. System.Environment.CurrentDirectory returns the current system catalog. In your case, AppDomain.CurrentDomain.BaseDirectory is the best solution.

+17
Mar 23 '09 at 19:24
source share

You must use AppDomain.CurrentDomain.BaseDirectory .

For example, in a Windows Services application:

System.Environment.CurrentDirectory will return C: \ Windows \ system32

Until

AppDomain.CurrentDomain.BaseDirectory will return [Application.exe location]

Another important factor to consider is that AppDomain.CurrentDomain.BaseDirectory is a readonly property, and Environment.CurrentDirectory can be something else if necessary:

 // Change the directory to AppDomain.CurrentDomain.BaseDirectory Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; 
+13
May 14 '14 at 17:55
source share

As I understand it, you should use BaseDirectory . CurrentDirectory may change during program execution.

+6
Mar 23 '09 at 19:24
source share

In Visual Studio 2010 test projects, if you enable the option to deploy test editing options, AppDomain.CurrentDomain.BaseDirectory points to the TestResults \ Out folder (not bin \ debug). Although, the default installation points to the bin \ debug folder.

Here I found a convincing answer.

Why doesn't AppDomain.CurrentDomain.BaseDirectory contain a β€œbin” in an asp.net application?

+3
Jul 23. '13 at 18:38
source share

I usually use something like:

  string AppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); AppPath = AppPath.Replace("file:\\", ""); 
+2
Mar 23 '09 at 19:24
source share

I also went through this a few days ago when I used

 Environment.CurrentDirectory 

since this was giving me a problem on a production server but working fine with my local server,

So, I tried using

 System.AppDomain.CurrentDomain.BaseDirectory; 

And he worked for me as in an environment.

So, since they all said that we should always go with

 System.AppDomain.CurrentDomain.BaseDirectory; 

as it checks the current domain directory for the path.

search for additional information

Could not find part of path error on server

+2
02 Sep '16 at 12:16
source share



All Articles