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

7

100

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 directory when we checked. So should I be using AppDomain.CurrentDomain.BaseDirectory or System.Environment.CurrentDirectory?

Thanks

Cp answered 23/3, 2009 at 19:20 Comment(2)
@Jade M How are you executing your exe1 at terminal?Thrower
I want to add my two cents by saying that string.GetFullPath(path) uses Environment.CurrentDirectory rather than CurrentDomain.BaseDirectory, to my surprise.Agglutinogen
S
201

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.

Sourdough answered 23/3, 2009 at 19:24 Comment(1)
I used button1 to show OpenFileDialog, then button2 to show Environment.CurrentDirectory, but still show debug folder, why?Ljubljana
H
22

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.

Horal answered 23/3, 2009 at 19:24 Comment(1)
To be clear, System.Environment.CurrentDirectory returns the current (process-specific) working directory.Seligmann
A
15

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;   
Aragon answered 14/5, 2014 at 17:55 Comment(0)
R
6

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

Re answered 23/3, 2009 at 19:24 Comment(0)
F
3

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?

Forethoughtful answered 23/7, 2013 at 18:38 Comment(0)
A
3

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

Alsworth answered 2/9, 2016 at 12:16 Comment(0)
V
2

I usually use something like:

            string AppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            AppPath = AppPath.Replace("file:\\", "");
Veliger answered 23/3, 2009 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.