2

Is it possible to get a list of the Special Folders in Windows 7 using Qt 4.7.4 I need to know in which directory the Operating System is installed and which folders I have write- access to. Special Folders will include folders like 'Desktop', 'Program Data', etc.... These folders may or may not be hidden.

I appreciate your time and response. Thank you in advance.

Dilshad
  • 61
  • 1
  • 4

3 Answers3

2

A few options:

  • Qt already has paths to many of these (cross-platform) in QDesktopServices. The method is QDesktopServices::storageLocation(StandardLocation).

  • For some, you can use qgetenv (as mentioned above).

  • If all else fails, you can directly call the SHGetSpecialFolderPath method in the Shell32 library. The list of possible options can be found on Microsoft's site.

Here is a sample of the last:

static QString getWindowsPath(int path_to_get)
{
    typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPWSTR, int, BOOL);

    QLibrary shell32_lib(QLatin1String("shell32"));
    GetSpecialFolderPath SHGetSpecialFolderPath =
        (GetSpecialFolderPath)shell32_lib.resolve("SHGetSpecialFolderPathW");

    QScopedPointer<wchar_t> w_path(new wchar_t[MAX_PATH]);
    SHGetSpecialFolderPath(0, w_path.data(), path_to_get, FALSE);

    return QString::fromWCharArray(w_path.data());
}

(Actually, SHGetSpecialFolderPath has been superseded by SHGetKnownFolderPath as of Vista, so if you know you are only targeting Windows 7, you should use that instead. It uses a KNOWNFOLDERID value.)

Dave Mateer
  • 17,608
  • 15
  • 96
  • 149
  • 3
    In Qt 5, the methods to get special folders have been moved from `QDesktopServices` to [`QStandardPaths`](http://qt-project.org/doc/qt-5.0/qtcore/qstandardpaths.html). – Jordan Miner Mar 13 '13 at 20:38
1

You could use the getenv from stdlib.

For example: You can find the path where the OS is installed under the environment variable windir.

Other examples:

  • APPDATA
  • COMPUTERNAME
  • PROGRAMFILES

You can find more examples here

Code example:

#include <stdlib.h>
#include <cassert>

int main( int argc, char* argv[] )
{
    char* programs_path = getenv("programfiles");

    assert( programs_path );

    return 0;
}

Remember to check if getenv returned null, especially for environment variables which you set yourself.

Exa
  • 4,020
  • 7
  • 43
  • 60
0

Try using QtGlobal::qgetenv. It get the environment variables, and here is the list of variables available on windows 7.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90