1

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?

Andy
  • 2,393
  • 4
  • 18
  • 20
  • possible duplicate of [How can i get the path of the current user's "Application Data" folder?](http://stackoverflow.com/questions/915210/how-can-i-get-the-path-of-the-current-users-application-data-folder) – Jonathon Reinhart Feb 05 '14 at 04:54
  • Thanks Jonathan, sometimes its just the right search string. I tried googling (using a different and much more verbose search string) and didn't get a proper link, so posted a question here. – Andy Feb 05 '14 at 05:38

4 Answers4

5

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

var appDataPath
    = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
3

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

Inisheer
  • 20,376
  • 9
  • 50
  • 82
2
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

or

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
Mayank
  • 8,777
  • 4
  • 35
  • 60
  • You may want to explain what's a difference b/w `ApplicationData` and `LocalApplicationData` – nam Dec 19 '16 at 00:03
0

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

Sarvesh Mishra
  • 2,014
  • 15
  • 30