windowswinforms

What is the path of "my computer" folder on windows?


I am working on a C++ windowsform project, using visual Studio IDE.
I use CFileDialog class to ask the user to select a file to open. It display an usual open file selection windows. I would like the default folder displayed to be the same as the one accessed when clicked on "My computer", where the harddrives, USB drives, dvd drives etc. are displayed.

I can define default folder by writting its path tolpstrInitialDir member, but I don't find the path for such a folder. I tried "\", "explorer.exe", "", none of them gave me the expected result. The application will be used by several users, so the solution must not include the user name in the path. i.e "C:\Documents and Settings[user]\Desktop\My Computer" may work but is not correct for my application.

Does anyone know of to define the "root" path of windows (i.e the root of C:\) ?

I searched on SO and internet but maybe I have used wrong keywords because I couldn't find appropriate content.


Solution

  • My Computer is a virtual shell folder that doesn't correspond to any file system directory. There's no file system path that would correspond to that location.

    Fortunately, file dialogs do speak "shellese", so you can use the CLSID (not to be confused with the GUID KNOWNFOLDERID or the CSIDL) of the shell folder. Sample in C# Winforms, but really, the only important part is the ::CLSID):

    var ofd = new OpenFileDialog();
    ofd.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}";
    ofd.ShowDialog();
    

    Disclaimer: I couldn't find any relevant documentation for the virtual folder CLSID, or this behaviour of the File dialog. So this is most likely not contractual, and could possibly change in future versions of Windows.