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

混江龙づ霸主 提交于 2019-12-17 15:38:52

问题


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


回答1:


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.




回答2:


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.



回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/1556082/as-a-developer-how-should-i-use-the-special-folders-in-windows-vista-and-windo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!