Andy
Andy

Reputation: 2593

How to get "Application Data" folder path programmatically in C#?

I have some legacy code that does some string concatenation to reach to the "Application Data" folder of the running PC. It has hard-coded strings like "C:\Documents and Settings\", "\Local Settings\Application Data\" etc.

The problem is it doesn't work on different versions of windows because of hard-coding.

Can I get this folder's path programmatically? May be by using an environment variable etc?

Upvotes: 1

Views: 2191

Answers (4)

Sarvesh Mishra
Sarvesh Mishra

Reputation: 2072

have you tried using

Environment.SpecialFolder.ApplicationData

It just gives you enumerated data.. use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) It gives you current user's AppData\Roaming folder

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66439

This will get the directory of the ApplicationData folder (or any other special system folder):

var appDataPath
    = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

Upvotes: 5

Mayank
Mayank

Reputation: 8852

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

or

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

Upvotes: 2

Inisheer
Inisheer

Reputation: 20794

Take a look at the Environment.SpecialFolder enum. There is one specifically for ApplicationData.

http://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx

Upvotes: 3

Related Questions