Find list of special folders in Windows with Qt

Is it possible to get a list of special folders in Windows 7 using Qt 4.7.4? I need to know in which directory the operating system is installed and in which folders I have write access. Special folders will include folders such as "Desktop", "Program Data", etc ... These folders may or may not be hidden.

I appreciate your time and response. Thanks in advance.

+2
source share
3 answers

Several variants:

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

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

  • If all else fails, you can directly call the SHGetSpecialFolderPath method in the Shell32 library. A list of options is available on the Microsoft website .

Here is an example of the latter:

 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 been replaced with SHGetKnownFolderPath since Vista, so if you know that you are targeting only Windows 7, you should use this instead. It uses the value KNOWNFOLDERID .)

+2
source

You can use getenv from stdlib.

For example: you can find the path in which the OS is installed under the windir environment variable.

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; } 

Be sure to check to see if getenv null, especially for environment variables that you set yourself.

+1
source

Try using QtGlobal :: qgetenv . It receives environment variables, and here is a list of variables available in Windows 7.

0
source

All Articles