Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get Windows %ProgramData% location?
QtWS25 Last Chance

How to get Windows %ProgramData% location?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 5.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Galbraith
    wrote on 25 May 2019, 22:19 last edited by
    #1

    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!

    A 1 Reply Last reply 26 May 2019, 06:17
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 May 2019, 22:23 last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply 25 May 2019, 22:42
      2
      • S SGaist
        25 May 2019, 22:23

        Hi and welcome to devnet,

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

        P Offline
        P Offline
        Paul Galbraith
        wrote on 25 May 2019, 22:42 last edited by
        #3

        @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?

        K 1 Reply Last reply 26 May 2019, 01:21
        0
        • P Paul Galbraith
          25 May 2019, 22:42

          @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?

          K Offline
          K Offline
          KillerSmath
          wrote on 26 May 2019, 01:21 last edited by
          #4

          @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"
          

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          1 Reply Last reply
          6
          • P Paul Galbraith
            25 May 2019, 22:19

            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!

            A Offline
            A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 26 May 2019, 06:17 last edited by aha_1980
            #5

            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

            Qt has to stay free or it will die.

            1 Reply Last reply
            4
            • P Offline
              P Offline
              Paul Galbraith
              wrote on 28 May 2019, 03:16 last edited by
              #6

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

              J 1 Reply Last reply 28 May 2019, 04:29
              1
              • P Paul Galbraith
                28 May 2019, 03:16

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

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 28 May 2019, 04:29 last edited by
                #7

                @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 :-)

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                3

                6/7

                28 May 2019, 03:16

                • Login

                • Login or register to search.
                6 out of 7
                • First post
                  6/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved