Search code examples
databasedelphidirectorytemporary

Where should I save a semi-temporary database?


I have an application where I would like to save some raw files in a folder structure on the client's computer. This EXE is completely stand-alone, and could possibly be running from a CD, or otherwise from a location which is read-only. Therefore, I cannot save my data in the EXE location. I would like to basically "ask" Windows where I should save the files, or save them in the proper location.

Where should I save this data on a client computer somewhat permanently but yet in a location where applications typically store their data? Since this application doesn't have an installer, I don't know how the permissions to certain program data folders will work out. I need an example of producing a common path for my application. Any instance of the app running on the same machine should know to look in this one dedicated location for this data.

PS - I mean semi-temporary database because the database is expected to stay in tact for a long period of time, but there's a possibility it may be erased to clean up, in which case it would automatically create a new one. The data its self can be considered somewhat of a cache.

EDIT

As far as windows user sessions, this is intended to be a global location for any windows session, and this application is actually a service which will be running in session 0.


Solution

  • Refer to MSDN CSIDL listing and use the SHGetFolderPath() function.

    Map the CSIDL CSIDL_COMMON_APPDATA to a physical directory.

    From MSDN:

    The file system directory that contains application data for all users. A typical path is C:\Documents and Settings\All Users\Application Data. This folder is used for application data that is not user specific. For example, an application can store a spell-check dictionary, a database of clip art, or a log file in the CSIDL_COMMON_APPDATA folder. This information will not roam and is available to anyone using the computer.

    Beneath this directory, create a directory named after the vendor (your company), and beneath that, a directory for your application. Then put your database in there.

    Note: Don't use CSIDL_COMMON_DOCUMENTS/ FOLDERID_PublicDocuments. This is for document-like files that can be shared across different applications. For proprietary data, like databases, use CSIDL_COMMON_APPDATA . Also files in CSIDL_COMMON_DOCUMENTS may be 'roamed' by the operating system - something you definitely don't want for a temporary database.

    Note: For Vista onwards use SHGetKnownFolderPath(), but for XP or cross-version, use SHGetFolderPath().