Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

exception

People also ask

What is System AppDomain CurrentDomain BaseDirectory?

AppDomain. CurrentDomain. BaseDirectory returns the directory from where the current application domain was loaded. System.

What is AppDomain and CurrentDomain?

The CurrentDomain property is used to obtain an AppDomain object that represents the current application domain. The FriendlyName property provides the name of the current application domain, which is then displayed at the command line.

How do I change my CurrentDirectory environment?

open System open System.IO if Environment. OSVersion. Platform = PlatformID. Win32NT then // Change the directory to %WINDIR% Environment.

What is current Directory in c#?

C# Program to Get Current Folder Path Using GetCurrentDirectory() Method. The method GetCurrentDirectory() is used to fetch the current folder path in which your working application is stored. In this case, it will fetch the directory from where our program runs.


If you want to find files in the same directory as your application, AppDomain.CurrentDomain.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.


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.


You should use AppDomain.CurrentDomain.BaseDirectory.

For example in a windows services application:

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

While

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

Another important factor to note is that AppDomain.CurrentDomain.BaseDirectory is a readonly property while the Environment.CurrentDirectory can be something else if necessary:

// Change the directory to AppDomain.CurrentDomain.BaseDirectory
Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;   

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


In Visual studio 2010 test projects, if you enable deployment option of Edit test settings, AppDomain.CurrentDomain.BaseDirectory points to the TestResults\Out folder(not bin\debug). Although, default setting point to bin\debug folder.

Here I found convincing answer.

Why AppDomain.CurrentDomain.BaseDirectory not contains "bin" in asp.net app?


I usually use something like:

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

I have also been through this few days back, as I was using

Environment.CurrentDirectory

as it was giving me issue on the production server but was working fine with my local server,

So, I tried with

System.AppDomain.CurrentDomain.BaseDirectory;

And it worked for me in both the Environment.

So, As all of them has said We should always go with

System.AppDomain.CurrentDomain.BaseDirectory;

as it checks the Current Domain directory for the path.

have a look for more information

Could not find a part of path error on server