PDA

View Full Version : How to find Application Data directory?



Teuniz
6th November 2007, 11:45
I want to save some program settings.
How do I know what the exact path is to "C:\Documents and Settings\user\Application Data" directory on windows?

wysota
6th November 2007, 11:58
You need to use WinAPI calls to get that or read appropriate values from Windows Registry (through QSettings), they are probably there somewhere...

blue.death
6th November 2007, 14:27
This is a possible solution (it's the same used in some Qt code):



#ifdef Q_WS_WIN
#include "qt_windows.h"
#include "qlibrary.h"
#ifndef CSIDL_COMMON_APPDATA
#define CSIDL_COMMON_APPDATA 0x0023 // All Users\Application Data
#endif
#ifndef CSIDL_APPDATA
#define CSIDL_APPDATA 0x001a // <username>\Application Data
#endif

QString MyClass::specialFolder(int type)
{
QString result;

QLibrary library(QLatin1String("shell32"));
QT_WA( {
typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPTSTR, int, BOOL);
GetSpecialFolderPath SHGetSpecialFolderPath = (GetSpecialFolderPath)library.resolve("SHGetSpecialFolderPathW");
if (SHGetSpecialFolderPath) {
TCHAR path[MAX_PATH];
SHGetSpecialFolderPath(0, path, type, FALSE);
result = QString::fromUtf16((ushort*)path);
}
} , {
typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, char*, int, BOOL);
GetSpecialFolderPath SHGetSpecialFolderPath = (GetSpecialFolderPath)library.resolve("SHGetSpecialFolderPathA");
if (SHGetSpecialFolderPath) {
char path[MAX_PATH];
SHGetSpecialFolderPath(0, path, type, FALSE);
result = QString::fromLocal8Bit(path);
}
} );

// Fallback on error
if (result.isEmpty()) {
switch (type) {
case CSIDL_COMMON_APPDATA:
result = QLatin1String("C:\\temp\\qt-common");
break;
case CSIDL_APPDATA:
result = QLatin1String("C:\\temp\\qt-user");
break;
default:
;
}
}

return result;
}


You could also use the Win32 API to look for the APPDATA environment variable or look in the registry for the appropriate keys (somewhere in Software\Microsoft\Windows\Current Version\...)

Bagstone
30th January 2008, 10:59
Hello first, although I'm reading quite a while on qtcentre.org, this is my first post! ;-)


I'd like to ask if this is really the only way?
I saved some settings for my application and now I thought about putting a log file there, but I want to limit it to a certain size (for example 1000 lines or even better a specific size like 50kb).

As the application directory isn't always writable for the current user, I thought it would be better to put it into the user's .config dir, or on windows, Application Data (language specific of course). Maybe I better use another folder or avoid the log file at all...


I'm just a bit uneasy about using this WINAPI thing and searching the registry and so on.
Additionally my project is a static binary to run on every PC without the need to ship the libraries with it and I don't know if this method might cause a problem to the static linker... but this is just a guess from a frightened programmer who experienced already too many problems ;-)

Teuniz
31st January 2008, 10:06
Additionally my project is a static binary to run on every PC without the need to ship the libraries with it and I don't know if this method might cause a problem to the static linker...

I can confirm that the above code from blue.death works fine with static linking.

przemoc
8th February 2008, 13:57
I want to save some program settings.
How do I know what the exact path is to "C:\Documents and Settings\user\Application Data" directory on windows?
Someday I asked myself similar question, but in platform independent way. My solution is not the prettiest one, but works.


// obtain (platform specific) application's data/settings directory
QSettings ini(QSettings::IniFormat, QSettings::UserScope,
QCoreApplication::organizationName(),
QCoreApplication::applicationName());
QString dir = QFileInfo(ini.fileName()).absolutePath();

dir string will contain:

on Windows - "%APPDATA%\<Organization Name>" ("<Drive>:\Documents and Settings\<login>\<Application Data>\<Organization Name>")
on Unix and Mac OS X - "$HOME/.config/<Organization Name>"

where <text> depends on your application & workspace (organization name, lingual version of Windows, etc.). This is a good place to put your data/settings without using QSettings.

jonny.dee
13th October 2011, 08:56
I know it's been a long time since this thread was created, but maybe my reply might still be useful for others.

You can also use QDesktopServices (http://doc.qt.nokia.com/latest/qdesktopservices.html) class to get the system paths.

Cheers,
Jonny

milgner
26th July 2013, 14:02
Another late update for anyone finding themselves here via Google or such:

This part of the QDesktopServices class has been replaced by QStandardPaths (http://qt-project.org/doc/qt-5.0/qtcore/qstandardpaths.html) in Qt 5.