Search code examples
appdata

Use different AppData folders for Debug and Release builds of an application


If I have an application named MyApplication, is it possible to have a custom location for the local AppData directory of this application? E.g.: C:\Users\User1\AppData\Local\MyApplication2. I can't find anywhere how to change this.

EDIT

Clarification: I don't want to create an auxiliary directoy called C:\Users\User1\AppData\Local\MyApplication2. I want the system to recognize C:\Users\User1\AppData\Local\MyApplication2 as the actual local AppData directory of MyApplication.

EDIT 2:

I really need a way to have both debug and release versions installed on my computer in order to be as close as possible to reality. Until now I managed to change where the application is installed and other application level identificators, but I couldn't change the AppData in order for each of them to use different settings files.


Solution

  • There's no need to move the location of {localappdata} if you're just wanting to change the location while developing your code on your machine. In fact, there's no need to get Inno Setup involved in this at all.

    Just define a local variable in your code based on the value of a compiler define (such as #IFDEF DEBUG, for instance). Create the %LOCALAPPDATA%\MyApplication2 folder during the installation. When DEBUG is defined, append to that location at runtime (which would be during development) to read/write your configuration info from a Debug folder.

    I don't know what language you're coding in, but in Delphi it would go something like this:

    {$IFDEF DEBUG}
    const
      ConfigDir = 'Debug\';
    {$ENDIF}
    
    // At application startup, retrieve the contents of %LOCALAPPDATA% via API call
    // or by retrieving the contents of the environmental variable (say into the
    // DataDir variable). Then...
    {$IFDEF DEBUG}
    DataDir := DataDir + ConfigDir;
    // If needed, you can check here for whether the folder exists and
    // create it if it doesn't.
    {$ENDIF}
    

    Now all of your code accessing the config info just retrieves it from DataDir, which will adjust based on whether you're in Debug or Release mode.