Results 1 to 8 of 8

Thread: How to find Application Data directory?

  1. #1
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    64
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to find Application Data directory?

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to find Application Data directory?

    You need to use WinAPI calls to get that or read appropriate values from Windows Registry (through QSettings), they are probably there somewhere...

  3. #3
    Join Date
    Jan 2006
    Posts
    14
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find Application Data directory?

    This is a possible solution (it's the same used in some Qt code):

    Qt Code:
    1. #ifdef Q_WS_WIN
    2. #include "qt_windows.h"
    3. #include "qlibrary.h"
    4. #ifndef CSIDL_COMMON_APPDATA
    5. #define CSIDL_COMMON_APPDATA 0x0023 // All Users\Application Data
    6. #endif
    7. #ifndef CSIDL_APPDATA
    8. #define CSIDL_APPDATA 0x001a // <username>\Application Data
    9. #endif
    10.  
    11. QString MyClass::specialFolder(int type)
    12. {
    13. QString result;
    14.  
    15. QLibrary library(QLatin1String("shell32"));
    16. QT_WA( {
    17. typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPTSTR, int, BOOL);
    18. GetSpecialFolderPath SHGetSpecialFolderPath = (GetSpecialFolderPath)library.resolve("SHGetSpecialFolderPathW");
    19. if (SHGetSpecialFolderPath) {
    20. TCHAR path[MAX_PATH];
    21. SHGetSpecialFolderPath(0, path, type, FALSE);
    22. result = QString::fromUtf16((ushort*)path);
    23. }
    24. } , {
    25. typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, char*, int, BOOL);
    26. GetSpecialFolderPath SHGetSpecialFolderPath = (GetSpecialFolderPath)library.resolve("SHGetSpecialFolderPathA");
    27. if (SHGetSpecialFolderPath) {
    28. char path[MAX_PATH];
    29. SHGetSpecialFolderPath(0, path, type, FALSE);
    30. result = QString::fromLocal8Bit(path);
    31. }
    32. } );
    33.  
    34. // Fallback on error
    35. if (result.isEmpty()) {
    36. switch (type) {
    37. case CSIDL_COMMON_APPDATA:
    38. result = QLatin1String("C:\\temp\\qt-common");
    39. break;
    40. case CSIDL_APPDATA:
    41. result = QLatin1String("C:\\temp\\qt-user");
    42. break;
    43. default:
    44. ;
    45. }
    46. }
    47.  
    48. return result;
    49. }
    To copy to clipboard, switch view to plain text mode 

    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\...)

  4. The following user says thank you to blue.death for this useful post:

    Teuniz (7th November 2007)

  5. #4
    Join Date
    Sep 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find Application Data directory?

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

  6. #5
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    64
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find Application Data directory?

    Quote Originally Posted by Bagstone View Post
    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.

  7. #6
    Join Date
    Jan 2008
    Location
    Warsaw, Poland
    Posts
    26
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    2

    Default Re: How to find Application Data directory?

    Quote Originally Posted by Teuniz View Post
    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.
    Qt Code:
    1. // obtain (platform specific) application's data/settings directory
    2. QSettings ini(QSettings::IniFormat, QSettings::UserScope,
    3. QCoreApplication::organizationName(),
    4. QCoreApplication::applicationName());
    5. QString dir = QFileInfo(ini.fileName()).absolutePath();
    To copy to clipboard, switch view to plain text mode 
    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.

  8. #7
    Join Date
    Aug 2011
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to find Application Data directory?

    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 class to get the system paths.

    Cheers,
    Jonny

  9. #8
    Join Date
    Jul 2013
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to find Application Data directory?

    Another late update for anyone finding themselves here via Google or such:

    This part of the QDesktopServices class has been replaced by QStandardPaths in Qt 5.

Similar Threads

  1. Installation of application data files
    By anderssonj in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2007, 17:02
  2. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 16:21
  3. Embedding binary data into an application
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 1st September 2006, 15:23
  4. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 13:53
  5. Am I the only one with "make" error ?
    By probine in forum Installation and Deployment
    Replies: 1
    Last Post: 13th February 2006, 13:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.