Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to get Windows %ProgramData% location?



  • I'm brand new to C++ and QT (having done a little C programming 30 years ago, and loads of Java programming in the meantime) ... just trying to wrap my head around a good way to determine the %ProgramData% directory location on Windows. I see there's a "Standard Locations" Qt widget, but it doesn't seem to have a way to return just this one directory. Is there an MS API call I need to make to get this location reliably? Keep in mind I'm very, very, new to C++ and these tools, so verbosity is helpful :-).

    Many thanks in advance!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You are likely looking for QStandardPaths. This class allows you to get information about various common paths cross-platform.



  • @SGaist Thanks for the quick reply, yes that is the widget I was looking at, but it's not clear to me how to use it to to return just the %ProgramData% directory, can you elaborate a little?



  • @Paul-Galbraith

    QStandardPaths class has a static method to get some standard dir paths for your application.
    QStringList QStandardPaths::standardLocations(QStandardPaths::StandardLocation type)

    That function pass the Type of the location to return a list of Locations related by the same.

    Take a look at the documentation, you will see an extensive table with the types and their respective results.
    https://doc.qt.io/qt-5/qstandardpaths.html#StandardLocation-enum

    The closest way to achieve your goal will be to pass AppConfigLocation type as parameter.

    [0] -> "C:/Users/<USER>/AppData/Local/<APPNAME>"
    [1] -> "C:/ProgramData/<APPNAME>"

    If your intention is only get the %ProgramData% then you can use QDir to apply a CD UP in this path.

    See below an example of how this could be implemented:

    // includes
    #include <QDebug>
    #include <QString>
    #include <QStandardPaths>
    #include <QDir>
    
    // code
    QString configDataDirString = QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation).at(1);
    QDir dir(configDataDirString); // "C:/ProgramData/<APPNAME>"
    dir.cdUp(); // cd ..
    qDebug() << dir.path(); // "C:/ProgramData"
    

  • Lifetime Qt Champion

    Hi @Paul-Galbraith,

    if your intention is really that Variable, and no cross-platform solution is needed, qgetenv or qEnvironmentVariable may also be helpful.

    Regards



  • Thank-you everyone for the helpful replies, I have decided to use qEnvironmentVariable to get the location.

    I should have mentioned that the reason I need this is to locate data files created by a different application, so I have no luxury in following best practices for portability, I and don't want to parse array results just to identify the directory I'm interested in. I'll sort out Windows for now and then deal with Mac later (in this case the app I'm interested in stores data under C:\Users<user> I believe for Macs).


  • Lifetime Qt Champion

    @Paul-Galbraith said in How to get Windows %ProgramData% location?:

    C:\Users<user> I believe for Macs

    MacOS is a UNIX and as such does not have drive letters :-)


Log in to reply