Storing Data Correctly in Modern Windows Systems

As of Windows Vista it became difficult to modify data stored in the Program Files directory. Although the system doesn't cause an error when a program writes data there, what actually happens is the data is not written to that location, but instead it is written to a location below the user directory. The location is typically something like: \User\AppData\Local\VirtualStore\Program Files\... Although that may not matter in a single-user system (though you may not be backing up the data correctly), in a multi-user system, where more than one person logs onto the same PC, that would mean that each user would have their own copy of the data, and changes from one would not appear in the data of the other.

The solution to this mess, is to store the data in a publicly accessible location. On Vista and later, that is the \Users\Public\Documents directory. There is a function in the application framework called getpublicdatadir() that can be used for this purpose. A small part of the initialization code from the sbapplication.new() method demonstrates how this is used in the SIMPOL Business application.

Example 7.5. The findcustomer() Function for the Orders Form

e = 0
datadir = getpublicdatadir(error=e)
if datadir <= ""
  wxmessagedialog(appw.w, "Error retrieving data directory", \
                  sAPPMSGTITLE, "ok", "error")
else
  dirsep = getdirectorysepchar()
  me.dirsep = dirsep
  me.startdir = trailingdirsep(getcurrentdirectory())
  datadir = trailingdirsep(datadir) + sAPPNAME + dirsep


The preceding fragment of code shows the approach. The datadir is constructed by combining the return value from getpublicdatadir() with the application name followed by the directory separator character. On most systems this will be: C:\Users\Public\Documents\SIMPOL Business\. The installer will have created this directory and copied the database files plus the SIMPOL Business.ini file into it.


Revision #6
Created 15 November 2021 18:51:28 by Eva Crawford-McKee
Updated 27 December 2021 21:37:30 by Nikolaus Zolnhofer