As a developer, how should I use the special folders in Windows Vista (and Windows 7)?
Asked Answered
M

5

25

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?!)?

Monetmoneta answered 12/10, 2009 at 18:18 Comment(0)
M
28

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. For example, everything where you want to ENSURE that it is only stored on this machine (like machine activation codes, often also cache/temporary data). Standard temporary files would go here.

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

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

Moiramoirai answered 12/10, 2009 at 18:34 Comment(2)
+1 for a helpful answer, particularly with regard to the "per user" distinction. But I'm not clear on what the names in bold are... literal names of folders? no... libraries? some kind of abstraction that .NET uses to refer to folders?Chalaza
@LarsH: "But I'm not clear on what the names in bold are..." They are .Net Environment.SpecialFolder enumeration constants - try clicking on them. :-)Mizzle
R
9

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<username>\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.
Rosariorosarium answered 12/10, 2009 at 18:44 Comment(0)
C
1

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.

Chronaxie answered 12/10, 2009 at 18:20 Comment(2)
I agree. I hate it when apps I install make directories and/or store files in (My) Documents.Durable
Can you translate your answer in terms of the Environment.SpecialFolder enumeration ? Environment.GetFolderPath(Environment.SpecialFolder.xxx)Monetmoneta
T
1

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.

Tessy answered 12/10, 2009 at 18:25 Comment(0)
B
0

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.

Bharat answered 22/10, 2009 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.