Inno Setup Edit installed file according to user p

2019-06-09 16:54发布

So I've been struggling with this problem for a few days now. Currently making an installer for our company software but the customer has to be able to fill in a URL that gets saved in the app.exe.config.

I've been through a lot of googling and found this piece of code that I edited.

var
  CustomEdit: TEdit;
  CustomPageID: Integer;

function LoadValueFromXML(const AFileName, APath: string): string;
var
  XMLNode: Variant;
  XMLDocument: Variant;  
begin
  Result := '';
  XMLDocument := CreateOleObject('Msxml2.DOMDocument');
  try
    XMLDocument.async := False;
    XMLDocument.load(AFileName);
    if (XMLDocument.parseError.errorCode <> 0) then
      MsgBox('The XML file could not be parsed. ' + 
        XMLDocument.parseError.reason, mbError, MB_OK)
    else
    begin
      XMLDocument.setProperty('SelectionLanguage', 'XPath');
      XMLNode := XMLDocument.selectSingleNode(APath);
      Result := XMLNode.text;
    end;
  except
    MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
  end;
end;

procedure SaveValueToXML(const AFileName, APath, AValue: string);
var
  XMLNode: Variant;
  XMLDocument: Variant;  
begin
  XMLDocument := CreateOleObject('Msxml2.DOMDocument');
  try
    XMLDocument.async := False;
    XMLDocument.load(AFileName);
    if (XMLDocument.parseError.errorCode <> 0) then
      MsgBox('The XML file could not be parsed. ' + 
        XMLDocument.parseError.reason, mbError, MB_OK)
    else
    begin
      XMLDocument.setProperty('SelectionLanguage', 'XPath');
      XMLNode := XMLDocument.selectSingleNode(APath);
      XMLNode.text := AValue;
      XMLDocument.save(AFileName);
    end;
  except
    MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
  end;
end;

procedure InitializeWizard;
var  
  CustomPage: TWizardPage;
begin
  CustomPage := CreateCustomPage(wpWelcome, 'Custom Page', 
    'Enter the new value that will be saved into the XML file');
  CustomPageID := CustomPage.ID;
  CustomEdit := TEdit.Create(WizardForm);
  CustomEdit.Parent := CustomPage.Surface;
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  if CurPageID = CustomPageID then
    CustomEdit.Text := LoadValueFromXML('C:\AutoScan.exe.config', '//configuration/system.serviceModel/client/endpoint/address');
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  Result := True;
  if CurPageID = CustomPageID then
    SaveValueToXML('C:\AutoScan.exe.config', '//configuration/system.serviceModel/client/endpoint/address', CustomEdit.Text);
end; 

and it does what is has to do if I specify an existing path like C:\AutoScan.exe.config but the setup starts complaining if the file doesn't exist. Of course the file only exists after it is installed. but in this case I want the file edited inside the installer I tried it with '{src}\AutoScan.exe.config' and '{app}\AutoScan.exe.config' but without result as the installer starts complaining it can't find the XML file

1条回答
我命由我不由天
2楼-- · 2019-06-09 17:37

You probably just need to edit the file after the installation completes.

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssPostInstall then
  begin
    SaveValueToXML(
      'C:\AutoScan.exe.config',
      '//configuration/system.serviceModel/client/endpoint/address', CustomEdit.Text);
  end;
end;

Also you should not load the value every time you get to the custom page, because you reset user preference, every time user returns back to the custom page.

You should load it only once in the InitializeWizard.

Either hard-code the default value.

Or if you really need to read it from the embedded file, you have to temporarily extract it.

procedure InitializeWizard;
var  
  CustomPage: TWizardPage;
begin
  CustomPage :=
    CreateCustomPage(
      wpWelcome, 'Custom Page',
      'Enter the new value that will be saved into the XML file');
  CustomEdit := TEdit.Create(WizardForm);
  CustomEdit.Parent := CustomPage.Surface;
  ExtractTemporaryFile('AutoScan.exe.config');
  CustomEdit.Text :=
    LoadValueFromXML(
      ExpandConstant('{tmp}\AutoScan.exe.config'),
      '//configuration/system.serviceModel/client/endpoint/address');
end;
查看更多
登录 后发表回答