开发者

Proper usage of the windows user profile directory

I need to create a directory inside a windows users 'home' directory (c:\Documents and Settings\someusername\ or c:\users\someusername\). This directory will exist permanently but will only contain temporary files.

What is is the best location for this directory within the users profile if I want to be a good citizen? I should note that my program will be run by (possibly) non-admin users and will only need access to their own profile, but they must have permission to create the folder.

Using My Documents\NameOfMyApp\ is possible I guess, but that seems intrusive.

Is there a better locatio开发者_C百科n for this type of data, and a specific MFC call to access it?


I'd consider using the AppData directory. You can get its location with SHGetSpecialFolderLocation passing it CSIDL_APPDATA; (or a number of alternatives -- nearly every version of Windows adds a new replacement for SHGetSpecialFolderLocation, SHGetSpecialFolderPath, or (often) both).


Take a look at the following win32 calls:

  • GetUserProfileDirectory
  • GetAllUsersProfileDirectory
  • GetDefaultUserProfileDirectory

You probably want to use GetUserProfileDirectory and put your data in a sub-directory with your appname.

You will definitely want to use the function because there is no "\documents and settings" folder on vista and up, they changed to it "\user".


Being a good application citizen, you should use: [drive]:\Documents and Settings[username]\Application Data[AppName] or [drive]:\Documents and Settings[username]\Local Settings\Application Data[AppName] (On Vista and Win7, "Documents and Settings" is replaced, most sensibly with "Users")

The environment variable USERPROFILE will provide the, you guessed it, User Profile Path. The TEMP path provides the path to the user's individual temp directory

If the temp files aren't user-specific, you could use C:\temp

EDIT: If you are to use a user-specific location, I highly recommend that you use the environment variables (USERPATH on XP and 2000) rather than hard-coding the paths.

-Waldo

P.S. Thank you for asking this. I see bad behaviour from Waaaay too many applications. The root of the C: drive is not where you should be dumping things! At the very least, (test for the presence of, creating if necessary, and) use C:\Temp.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜