Where to save application data in windows?

471 views Asked by At

I am trying to make a windows application. In this application, some files get modified as a user add or delete an entry. I saved these files on the application folder itself. But After making binary file I installed it, As I try to add a entry it get crashed.

So, I figured out the issue. The windows doesn't allow to modified files inside C:\Program Files. So, I installed it in other drive and it works. It solved my issue temporarily but I want to know how other application works in windows.

Where do those applications save their data? I am not talking about some data which get saved in "Documents" but something which is essential need to modified every time user makes change like theme, formates.

1

There are 1 answers

0
musicamante On

No user access is allowed to the "program folder", and that's for good: it is a system folder, and it should only be accessed for system related operations (like installing or uninstalling a program).

There are many places where "program data" can be stored depending on the situation, and QStandardPaths provides access to their paths, according to the category location. What you might be interested in are:

  • ConfigLocation: Returns a directory location where user-specific configuration files should be written. This may be either a generic value or application-specific, and the returned path is never empty.
  • AppDataLocation: Returns a directory location where persistent application data can be stored. This is an application-specific directory.
  • AppLocalDataLocation: As the previous one, but Windows specific.
  • AppConfigLocation: Returns a directory location where user-specific configuration files should be written. This is an application-specific directory, and the returned path is never empty.

Those paths (along with the others listed in the documentation) can be accessed using the following static methods:

If you need to store the user configuration, you can use QStandardPaths.writableLocation(AppConfigLocation), while if you have some user-specific internal data that is used by the application (email database, document templates, etc) QStandardPaths.writableLocation(AppLocalDataLocation) should be a good choice.

In both cases, those paths may not exist, so you need to ensure that and eventually create them, possibly by using QDir(path):

    dataPath = QtCore.QStandardPaths.writableLocation(AppLocalDataLocation)
    dataPathDir = QtCore.QDir(dataPath)
    if not dataPathDir.exists():
        # create the directory (including parent directories if they don't exist);
        # that the argument of mkpath is relative to the QDir's object path, so
        # using '.' means that it will create the actual dataPath
        dataPathDir.mkpath('.')

Note that for all of the above (especially the last 3) it's required that you correctly set both the organizationName and the applicationName.