views:

39

answers:

3

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 location for this type of data, and a specific MFC call to access it?

+1  A: 

Take a look at the following win32 calls:

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".

Byron Whitlock
+4  A: 

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).

Jerry Coffin
You also want to consider whether this data should "roam" or be local to the machine.
Adrian McCarthy
The MSDN (http://msdn.microsoft.com/en-us/library/bb762494%28VS.85%29.aspx) seems to indicate that the CSIDL values should not be used for new development, however I have to support clients as far back as Windows 2000 and right up to Windows 7. Will `CSIDL_APPDATA` work on Windows 7?
theycallmemorty
@theycallmemorty: You might consider `ShGetFolderLocation` instead (was new with Win2K, though it still uses a CSIDL). A quick check shows that `SHGetSpecialFolderLocation(CSIDL_APPDATA)` still seems to work on Win7.
Jerry Coffin
+1  A: 

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.

gWaldo
IMO, this borders on needing a downvote. Just for example, on Vista or newer, there won't normally be a `[drive:\Dcouments and Settings`. USERPROFILEPATH is *better*, but still far from ideal (far too easy for it to get screwed up, since the use can edit, delete, etc., environment variables).
Jerry Coffin
Ok, that's fair. However on XP and lower (rough guess ~= 60% of the enterprise desktops), it is applicable.If I'd had a Vista or Win7 machine, I'd have included the new answer.I'll edit my comment to recommend using the environment variable instead of hard-coding the path.
gWaldo