Solved

How to retrieve the user's "My Documents" folder

Posted on 2006-06-14
23
1,094 Views
Last Modified: 2012-05-05
I want to copy some document templates to the user's "My Documents" folder at setup time. In my C++ program I need to read from that folder. Though the folder mostly is located at "%userprofile%\My Documents" there is a chance that the user moved it to some other path.

I found out that the path of the folder was stored in registry at "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal". I easily could read that value in C++.

I also found a VB code snippet by googling that retrieves the folder name by

   from win32com.shell import shell
   df = shell.SHGetDesktopFolder()
   pidl = df.ParseDisplayName(0, None,  
    "::{450d8fba-ad25-11d0-98a8-0800361b1103}")[1]
   mydocs = shell.SHGetPathFromIDList(pidl)

I am weak in VB but I think I would be able to convert the snippet to C++.

I have a few questions related to that:

1. Are these solutions equivalent and valid? Means, do they always retrieve a valid path name?
2. Isn't there an easier solution, e. g. using a predefined name like %mydocs%?
3. If not, does anyone know another solution that can be used in a batch file?

Thanks in advance

Regards Alex
0
Comment
Question by:itsmeandnobodyelse
  • 10
  • 7
  • 6
23 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 16901224
You should be able to use SHGetSpecialFolderLocation
0
 
LVL 30

Accepted Solution

by:
Axter earned 400 total points
ID: 16901229
Here's a copy and paste code example from another PAQ:

#include <windows.h>
#include <shlobj.h>
#include <shlwapi.h>

bool GetDesktopFolder (char *szDesktopDir)
{
    HRESULT hr;
    ITEMIDLIST *pidl;

    hr = SHGetSpecialFolderLocation (0,
              CSIDL_DESKTOPDIRECTORY, &pidl);
    szDesktopDir[0] = '\0';
    if (hr == NOERROR)
    {
        BOOL rtn;
        rtn = SHGetPathFromIDList (pidl, szDesktopDir);
        return rtn == TRUE ? true : false;
    }
    return false; // SHGetSpecialFolderLocation failed
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 16901238
For your requirements, you'll want to use CSIDL_MYDOCUMENTS  or CSIDL_COMMON_DOCUMENTS.

    hr = SHGetSpecialFolderLocation (0,
              CSIDL_MYDOCUMENTS, &pidl);
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 30

Expert Comment

by:Axter
ID: 16901243
For this code you need shell32.lib, and you should have the latest SDK installed.
0
 
LVL 39

Author Comment

by:itsmeandnobodyelse
ID: 16901249
>>>> SHGetSpecialFolderLocation

Unfortunately, SHGetSpecialFolderLocation has no predefined index for the "My Documents" folder. There is no id like CSIDL_DESKTOPDIRECTORY.

Regards, Alex


0
 
LVL 39

Author Comment

by:itsmeandnobodyelse
ID: 16901272
>>>> CSIDL_MYDOCUMENTS  

That id doesn't exist on my VC6 system. Does it come with VC7 or VC8?

Did you know of CSIDL_PERSONAL?

Regards, Alex


0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 100 total points
ID: 16901278
Looking at http://technet2.microsoft.com/WindowsServer/en/Library/bdfedadf-0e81-43b8-befc-a8a003810c751033.mspx?mfr=true you don't appear to have the definitive way to find My Documents, though you'd expect it always to work. Seems to me like your  HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal setting is a better bet.
0
 
LVL 39

Author Comment

by:itsmeandnobodyelse
ID: 16901294
>>>> and you should have the latest SDK installed.

That might become a problem cause it's a customer's environment.

Do you know whether the registry solution (Q1) is valid?

Regards, Alex


0
 
LVL 39

Author Comment

by:itsmeandnobodyelse
ID: 16901313
>>>> Seems to me like your ... setting is a better bet.

I found the folder path in

   HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal

and

   HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal


What do you think is the better one?

Regards, Alex
0
 
LVL 39

Author Comment

by:itsmeandnobodyelse
ID: 16901323
>>>> CSIDL_PERSONAL

I found it in shlobj.h. I'll give it a try.

Regards, Alex
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16901334
"User Shell Folders" is the per user one, methinks. Presumably "Shell Folders" is for any user created by the user?? Sorry, I'm guessing Alex.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16901336
>>That id doesn't exist on my VC6 system. Does it come with VC7 or VC8?

As I stated, you need to have the latest SDK on your machine.
CSIDL_MYDOCUMENTS works with VC6 if you have the latest SDK installed.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16901347
>>you don't appear to have the definitive way to find My Documents

You do have a definitve way.  Just use CSIDL_MYDOCUMENTS.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16901357
0
 
LVL 30

Expert Comment

by:Axter
ID: 16901366
>>Did you know of CSIDL_PERSONAL?

I've never used it, but here's what MSDN saids about it:
CSIDL_PERSONAL (0x0005)
The file system directory used to physically store a user's common repository of documents. A typical path is C:\Documents and Settings\username\My Documents. This should be distinguished from the virtual My Documents folder in the namespace, identified by CSIDL_MYDOCUMENTS. To access that virtual folder, use SHGetFolderLocation, which returns the ITEMIDLIST for the virtual location, or refer to the technique described in Managing the File System.

From above statement, it's not very clear to me what's the difference between CSIDL_PERSONAL and CSIDL_MYDOCUMENTS.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16901373
>>but I guess there still exist Windows 98 users.
You could add extra logic in the code for Win9x machines.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16901374

>>>you don't appear to have the definitive way to find My Documents

>You do have a definitve way.  Just use CSIDL_MYDOCUMENTS.

I meant using 450d8fba-ad25-11d0-98a8-0800361b1103, which is what the link refers to. I wan't referring to your idea, David, which I hadn't seen, when I posted. I had browser lag, because my cat had kittens while I had Alex's initial post open :-)
0
 
LVL 39

Author Comment

by:itsmeandnobodyelse
ID: 16901391
>>>> You do have a definitve way.  Just use CSIDL_MYDOCUMENTS.

It works with CSIDL_PERSONAL.

>>>> Sorry, I'm guessing Alex.

No problem. It's most likely redundant.

I will use Axter's way for the programmatical access and the registry way for the batch (cause I already have a prog that was able to assign an environtment variable with a registry entry).

Regards, Alex


0
 
LVL 30

Expert Comment

by:Axter
ID: 16901490
Are you guys getting a major lag for email notification?
I'm getting email notifications about 15-30 minutes later.

I'm not sure if it's my server, or EE.
0
 
LVL 39

Author Comment

by:itsmeandnobodyelse
ID: 16901540
>>>> Are you guys getting a major lag for email notification?

No, the lags I had in the thread here have been due to typing lags. My notifications have been in the same minute as in EE.

Regards, Alex

0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16901634
> Are you guys getting a major lag for email notification?

It's quick for me too.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16902318
Must be my server then.

Thanks
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16902394
You may be able tell where the hold-up is by looking at the chain of "Received:" headers (weirdly this is View | Options in MS Outlook).
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question