• 10
• 9
• 13
• 10
• 18

# [Win32] Clean "My Documents" access

This topic is 4303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've only done a bit of Win32 programming before, but I'm finally writing a decent save-game module for my games and I'm trying to follow filesystem conventions on all platforms -- I've got the OS X conventions done so far and I was wondering about Windows. I want to use the "My Documents/My Games" folder to dump my savegames in. I use the standard iostream, etc methods of producing files from C++; is there a fast way to get the current user's "My Documents" path from Win32 so I can create the save game folder inside it? On Unix, there's the ~ shortcut to get right to a user's home directory (for example, ~/Documents maps to /Users/whoever/Documents for the current user); I assume a similar thing exists in Windows. I should be able to figure out how to create directories from there. If no such in-path shortcut exists (Does %HOME% work?) I assume there's a Win32 API function to get the path. Thanks in advance. EDIT: Nevermind, I found it. It's %USERPROFILE% according to my XP SP2 box here. Is this consistent for all versions of Windows?

##### Share on other sites
You want something like the SHGetFolderPath Function?

EDIT : This function is standard from win98!

##### Share on other sites
I believe this MSDN page is what you're looking for. Use a CSIDL of CSIDL_MYDOCUMENTS to get the "My Documents" folder.

Edit: Too slow...

And while you could use %USERPROFILE%, I wouldn't rely on environment variables when the Shell functions are guaranteed to return the correct paths.

##### Share on other sites
The environment variable doesn't seem to work anyway; it doesn't get converted properly from the CreateDirectory call because it's not interpreted by the shell. Doy.

Now I have to learn what header file the shell functions are in.

##### Share on other sites
Its in shlobj.h. Most of the msdn posts list the header,library and OS necessary for the method to work. And often they have a handy code sample showing you how to use it.

Cheers
Chris

##### Share on other sites
Yeah, I figured that out by grepping my includes directory. Anyway, it seems to work now. Thanks, guys. [grin]

Now I just have to get back to my Mac to test out the OS X version of the code.

##### Share on other sites
Quote:
 Original post by RavuyaI want to use the "My Documents/My Games" folder to dump my savegames in.

Please, don't do that. I absolutely hate games which put saves into documents since (especially games with lots of big saves). I have small partition for documents only (simplifies backups) and because of this I had to create link to the another partition in the documents. I think, the best way is to let the user choose save directory and default option may be into My Documents/My Games. Then whoever cares can change it, other people will just leave the default settings.

##### Share on other sites
Quote:
Original post by b2b3
Quote:
 Original post by RavuyaI want to use the "My Documents/My Games" folder to dump my savegames in.

Please, don't do that. I absolutely hate games which put saves into documents since (especially games with lots of big saves). I have small partition for documents only (simplifies backups) and because of this I had to create link to the another partition in the documents. I think, the best way is to let the user choose save directory and default option may be into My Documents/My Games. Then whoever cares can change it, other people will just leave the default settings.

I have a problem with the suggested path, but more for the reason that it's non-standard. Use the settings folder, not the document folder.
X:\Documents and Settings\Users\Local Settings\...

##### Share on other sites
Quote:
Original post by b2b3
Quote:
 Original post by RavuyaI want to use the "My Documents/My Games" folder to dump my savegames in.

Please, don't do that. I absolutely hate games which put saves into documents since (especially games with lots of big saves). I have small partition for documents only (simplifies backups) and because of this I had to create link to the another partition in the documents. I think, the best way is to let the user choose save directory and default option may be into My Documents/My Games. Then whoever cares can change it, other people will just leave the default settings.

Actualy if you ever want to have the windows xp logo on your product you MUST save your savegames inside the Documents and settings/application data folder otherwise microsoft will not qualify it as an nice windows application.

##### Share on other sites
Quote:
 Original post by TjaalieActualy if you ever want to have the windows xp logo on your product you MUST save your savegames inside the Documents and settings/application data folder otherwise microsoft will not qualify it as an nice windows application.

MS Word saves document files to the My Documents folder by default. A document file is essentially a method of storing the user's progress so they can continue from the same point later. Word also stores some settings, which get put into the settings/application data folder.

Some games save savefiles to the My Documents folder by default. A save file is essentially a method of storing the user's progress so they can continue from the same point later. Games also store some settings, which get put into the settings/application data folder.

Essentially, I disagree with you on the grounds that a save-file is neither application data nor a configuration file, it is a file which belongs to the user, and there's no (good) reason you can't have it open from the shell and even display a thumbnail in Explorer.

Quote:
 Original post by benryvesI have a problem with the suggested path, but more for the reason that it's non-standard.

Microsoft use it. Microsoft designed the OS. How is it non-standard? Again, a savefile isn't really application data, so why the choice of the settings folder?

Definately try to include some way of changing the default whatever you decide.