How to find directory level information with C# .NET

In this post we saw how to extract system-level information using the Environment class in .NET. Another group of methods and properties of the Environment class lets you easily extract directory level information. Here come a couple of examples.

Here’s how to find the directory that the application is currently running in:

string currentDirectory = Environment.CurrentDirectory;

If you call the above method from a .NET app in Visual Studio then this will point to the deploy folder configured for the project, e.g. “C:\TestProjects\VariousCSharpLanguageConstructs\Various\Various\bin\Debug”

The GetFolderPath method accepts an Environment.SpecialFolder enumeration. Using this method will help you easily get a full file path reference to some well-known Windows folders like the MyDocuments folder:

string myDocumentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string startMenuFolder = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
string userProfileFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

It’s worth exploring the SpecialFolder enumeration and see what’s available there using IntelliSense.

Finally here’s how to find the location of the System directory:

string systemDirectory = Environment.SystemDirectory;

In my case it’s c:\Windows\system32

View all posts related to diagnostics here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

2 Responses to How to find directory level information with C# .NET

  1. Michael Schmid says:

    Be careful using “Environment.CurrentDirectory”, when executing application as a service. Then it results in “C:\WINDOWS\system32” (when inherited form “WindowsServiceNET”). In this case “AppDomain.CurrentDomain.BaseDirectory” is useful.
    I enjoy your articles all the time. Keep it up šŸ˜‰

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

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT BEST PRACTICES WITH MICROSOFT STACK & ANGULAR

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: