6 ways to get the current directory in C#

System.AppContext.BaseDirectory

This is the prefered replacement for AppDomain.CurrentDomain.BaseDirectory in .net core (at least until the API appears for AppDomain, if it ever will).

AppDomain.CurrentDomain.BaseDirectory

This is the best option all round. It will give you the base directory for class libraries, including those in ASP.NET applications.

Directory.GetCurrentDirectory()

This does an interop call using the winapi GetCurrentDirectory call inside kernel32.dll, which means the launching process’ folder will often be returned. Also as the MSDN documents say, it’s not guaranteed to work on mobile devices.

Environment.CurrentDirectory

This simply calls Directory.GetCurrentDirectory()

Assembly.Location

This would be called using

this.GetType().Assembly.Location

This returns the full path to the calling assembly, including the assembly name itself. If you are calling a separate class library, then its base directory will be returned, such “C:\myassembly.dll” – depending obviously on which Assembly instance is being used.

Application.StartupPath

This is inside the System.Windows.Forms namespace, so is typically used in window forms application only.

Application.ExecutablePath

The same as Application.StartupPath, however this also includes the application name, such as “myapp.exe”

[ copy from : http://www.anotherchris.net/csharp/6-ways-to-get-the-current-directory-in-csharp/ ]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.