2
votes

I am trying to add a folder to an installation which will eventually hold user output data. I can't put a folder into Program Files because users will not have the required permissions to write to it.

If it is not being installed to Program Files, then the data folder can be created inside the application folder (this is working fine).

I have a little piece of code to detect whether the installation was made to Program Files and, if so, I wanted to use CreateDir() to make a data folder in C:\Users\Public\Documents\{'MyAppName}\DB This seems to fail, in [Code] even though the standard Inno Setup script works:

[Dirs]
Name: "{commondocs}\{#MyAppName}\DB"

I am using the DeinitialiseSetup() procedure to make this happen at the end of installation, once the path is definite.

This is my code:

[Code]
  procedure DeinitializeSetup();
  begin
    { If it has been installed in the Program Files Folder put DB in Public Documents }
    if Pos(ExpandConstant('{pf}'),ExpandConstant('{app}')) > 0 then                       
    begin
      if not CreateDir (ExpandConstant('{commondocs}\{#MyAppName}\DB')) then
        MsgBox('Error: Data folder could not be created.', mbInformation, MB_OK);
    end
      else
    begin
      if not CreateDir (ExpandConstant('{app}\DB')) then
        MsgBox('Error: Data folder could not be created.', mbCriticalError, MB_OK);
     end;
  end;

Following another SO suggestion I used:

PrivilegesRequired=lowest

in the script but it did not work with or without this. I am beginning to think this may be a permissions issue but am not sure why, as the installer standard [Dirs] script works fine.

This is not the same as the other questions regarding identifying the path - I have got all the paths I want, only: [Code] CreateDir() does not seem able to create a folder in {commondocs}.

Many thanks for any suggestions.

1
@MartynPrikryl Many thanks for making that so much better with your edits.Steve

1 Answers

2
votes

My guess is that the {commondocs}\{#MyAppName} does not exist. CreateDir function can create a single directory only. It won't create parent folders for you, if they do not exist (contrary to [Dirs] section entry).

You can use ForceDirectories function instead:

Creates all the directories along the specified directory path all at once.


Side note: Do not use DeinitializeSetup to create the directories – Is is triggered even if the installation fails, or even if the user cancels the installation.

Use CurStepChanged(ssPostInstall):

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssPostInstall then
  begin
    { Your code }
  end;
end;