BaseDirectory vs CurrentDirectory in C#

I got this question while replying in a forum. This may not be a new or exciting topic but I'm sharing the reason behind this article. It is to explain to that person better as well as all others alike.

BaseDirectory belongs to the CurrentDomain property of the AppDomain Class whereas CurrentDirectory belongs to the Environment class. Both properties are self-explanatory. The Environment class exposes all the information related to the current context/environment and platform whereas AppDomain represents an application domain, which is an isolated environment where the application executes. I'm not going to discuss the AppDomain & Environment class since it would be off-topic.

To expose the differences in between, I created a sample demo console application. Please refer to the picture below. Note that the name of the application is "SampleApp" and the Path of the application is "C:\Users\Sunny\Documents\Visual Studio 2012", since you will want to check the output with the actual path.

Directory1.jpg

And the following few lines of codes are for the Program.cs file:

Directory2.jpg

The first line prints the BaseDirectory of the application.

BaseDirectory returns the base directory that the assembly resolver uses to probe for the assemblies or where the executable file lies, whereas, CurrentDirectory returns the Current Working Directory in very simple means. It returns the path inside where you're executing the application.

So, going further, the following is what I get after running the application:

Directory3.jpg

What I see here is output for both the Paths are the same as in the following:

"C:\Users\Sunny\Documents\Visual Studio 2012\SampleApp\SampleApp\bin\Debug"

A little confusing, no? Follow along.

Now I start a separate Command Prompt and run the application right from the default location, "C:\Users\Sunny". See what I get:

Directory4.jpg

Here for BaseDirectory it returns the same path, but for CurrentDirectory, the location is where I'm right now, in the command prompt, in other words: "C:\Users\Sunny". Hope you have gotten the point No? Ok, one more step.

To demonstrate a little more, I changed my working directory to "D:\Sunny" now and run the application from in there, see what I get in return:

Directory5.jpg

For BaseDirectory, it's still the same as before:

"C:\Users\Sunny\Documents\Visual Studio 2012\SampleApp\SampleApp\bin\debug"

For CurrentDirectory however I get "D:\Sunny" and that's what my current working directory is. So, the conclusion is that the BaseDirectory returns the path where the executable file exists, whereas, CurrentDirectory returns the path that the current working directory from the executable file is being called. I hope you now understand very clearly the difference between both the Paths returned.

Thanks for reading. Feedback and comments are welcome & appreciated as always!!
 

Up Next
    Ebook Download
    View all
    Learn
    View all