tags:

views:

2409

answers:

5

There are several possible ways of getting the path to the application data directory:

  • using the %APPDATA% environment variable
  • calling SHGetFolderPath with CSIDL_APPDATA

What is the best way to get the path from within an program? Are there any gotchas when I use the environment variable?

Which method is safest across XP, Vista and upcoming versions?

+10  A: 

I would suggest that calling SHGetFolderPath() is the most appropriate, and portable method; the alternatives, such as reading an environment variable, or (worse) trying to extract it from the registry are likely to trip you up in the future.

Raymond Chen has an article explaining why pulling such paths from the registry is a bad idea.

Rob
I read the article, and I previously I did exactly what the article told me *not* to do:)
Torsten Marek
+1  A: 

If you're programming in .NET you can use this:

string appDataPath = Environment.SpecialFolder.ApplicationData;
Jason Miesionczek
SpecialFolder.ApplicationData is an enum, not a path. Use Environment.GetFolderPath as shown in @jMM's answer
JulianM
A: 

One important difference in Python: in case of unicode file paths ctypes.windll.shell32.SHGetFolderPathW returns a unicode string, whereasos.environ['APPDATA'] returns a byte string.

krawyoti
+5  A: 
    string appDataPath = 
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); 

You'll need to use the GetFolderPath method get the actual path as Environment.SpecialFolder.ApplicationData is just an enum.

jMM
A: 

can somone please assist me in at getting rid of these error messages: Error message 1606; and error message %APPDATA%\ ?

edward
I think you should make a new question for that.
Torsten Marek