views:

474

answers:

5

Where should I save data related to my application? Where should I save configuration files? Where should I save temporary files? Should I do it in "Documents" ? "AppData" ? etc.... What is the best practice for saving data to the disk (I guess, best practice for special folders?!) Thanks!

+1  A: 

AppData should be automatic configuration and profiles, documents should only be user saved files. I would put anything that could be useful in multiple systems in AppData/Roaming, and anything else (machine specific) in AppData/Local or LocalLow. For temporary files, use the Temp directory.

James Deville
I agree. I hate it when apps I install make directories and/or store files in (My) Documents.
Thomas Owens
Can you translate your answer in terms of the Environment.SpecialFolder enumeration ? Environment.GetFolderPath(Environment.SpecialFolder.xxx)
Nestor
A: 

In general, Windows best practices are to follow the appropriate environment variables. Configuration data that you want to stick around should go in %APPDATA%, and temporary files should go in %TEMP%.

This has the advantage of not breaking horribly if Windows changes things, and respecting strange configurations people may have.

Also, I agree with other answers that "Documents" should only be for user-created data and only after a prompt.

Clueless
+12  A: 

ApplicationData: Everything that your application needs as "per user" data and does not fall under other categories. Standard configuration files would go here.

CommonApplicationData: Everything that is not "per user" data.

LocalApplicationData: Data that is per user and non-roaming. E.g. everything where you want to ENSURE that it is only stored on this machine (like machine activation codes, often also cache/temp data). Standard temp files would go here.

MyDocuments: User data that the user actually would identify as "recognizable single documents"

Addition: If you don't care about the filename you can also use a tempfile API to generate a temp file in the temp directory. You should NOT do this manually. In e.g. .Net you can use Path.GetTempFileName() for that purpose.

Foxfire
+5  A: 

From Windows 7 Client Software Logo Program:

  • Applications should be installed to the Program Files folder by default. User data or application data must never be stored in this location because of the security permissions %ProgramFiles% for native 32-bit and 64-bit applications, and %ProgramFiles(x86)% for 32-bit applications running on x64 respectively overwriting each other's data and settings.
  • All application data that must be shared among users on the computer should be stored within ProgramData
  • All application data exclusive to a specific user and not to be shared with other users of the computer must be stored in Users\\AppData
  • Never write directly to the "Windows" directory and or subdirectories. Use the correct methods for installing files, such as fonts or drivers
  • In “per-machine” installations, user data must be written at first run and not during the installation. This is because there is no correct user location to store data at time of installation. Attempts by an application to modify default association behaviors at a machine level after installation will be unsuccessful. Instead, defaults must be claimed on a per-user level, which prevents multiple users from overwriting each other's defaults.
Remus Rusanu
A: 

In addition you should use SHGetFolderPath or SHGetKnownFolderPath (in Vista+) to get the paths. This will provide you the correct location on the current system regardless of the version or the language of the current installation. Never hard code any path you are using.

Foxeris