如何从Innosetup脚本修改Local.config

时间:2015-08-07 01:18:53

标签: inno-setup pascalscript

我遇到一些问题让这段代码正常运行。我想要完成的是用户放入一个ConnectionString(在安装过程中)和它在已经格式化的文件中的位置。该文件是{app} \ local目录中的Local.config。我能够在{app}导演中让它工作几次,但现在我什么都没有。

Source: "Local\Local.config"; DestDir: "{app}\Local"; AfterInstall: ConvertConfig('Local.config'); Flags: ignoreversion

var
  UserPage: TInputQueryWizardPage;
  xmlFileName: String;

procedure InitializeWizard;
begin

    UserPage := CreateInputQueryPage(wpWelcome,
    'Pryme Connection String', 'SQL Connecton String', 'Please Enter In SQL Connection String then click Next.');
    UserPage.Add('connectionString:', False);

end;
procedure ConvertConfig(xmlFileName: String);
    var
        xmlFile: String;
        xmlInhalt: TArrayOfString;
        strName: String;
        strTest: String;
        tmpConfigFile: String;
        k: Integer;
    begin
             xmlFile := ExpandConstant('{app}') + '\' + 'Local.config';
         tmpConfigFile:= ExpandConstant('{app}') + '\config.tmp';
         strName :=  UserPage.Values[0];

          if (FileExists(xmlFile)) then begin
            //alles in string array speichern
             LoadStringsFromFile(xmlFile, xmlInhalt);
          //durch Array iterieren
            for k:=0 to GetArrayLength(xmlInhalt)-1 do
              begin
              strTest := xmlInhalt[k];
                  if (Pos('name="Pryme"', strTest) <> 0 ) then
                  begin
                        strTest := '  <add name="Pryme" connectionString="' + strName + '"/> ';
                  end;
              SaveStringToFile(tmpConfigFile, strTest + #13#10,  True);
          end;

          DeleteFile(xmlFile); //delete the old exe.config
          RenameFile(tmpConfigFile,xmlFile);

          end;
    end;

1 个答案:

答案 0 :(得分:-1)

I didn't point to the correct directory.Below is the correct working code.

 Source: "Local\Local.config"; DestDir: "{app}\Local"; AfterInstall: ConvertConfig('Local.config'); Flags: ignoreversion



    var
      UserPage: TInputQueryWizardPage;
      xmlFileName: String;

    procedure InitializeWizard;
    begin

        UserPage := CreateInputQueryPage(wpWelcome,
        'Pryme Connection String', 'SQL Connecton String', 'Please Enter In SQL Connection String then click Next.');
        UserPage.Add('connectionString:', False);

    end;
    procedure ConvertConfig(xmlFileName: String);
        var
            xmlFile: String;
            xmlInhalt: TArrayOfString;
            strName: String;
            strTest: String;
            tmpConfigFile: String;
            k: Integer;
        begin
                 xmlFile := ExpandConstant('{app}\local') + '\' + 'Local.config';
             tmpConfigFile:= ExpandConstant('{app}\local') + '\config.tmp';
             strName :=  UserPage.Values[0];

              if (FileExists(xmlFile)) then begin
                //alles in string array speichern
                 LoadStringsFromFile(xmlFile, xmlInhalt);
              //durch Array iterieren
                for k:=0 to GetArrayLength(xmlInhalt)-1 do
                  begin
                  strTest := xmlInhalt[k];
                      if (Pos('name="Pryme"', strTest) <> 0 ) then
                      begin
                            strTest := '  <add name="Pryme" connectionString="' + strName + '"/> ';
                      end;
                  SaveStringToFile(tmpConfigFile, strTest + #13#10,  True);
              end;

              DeleteFile(xmlFile); //delete the old exe.config
              RenameFile(tmpConfigFile,xmlFile);

              end;

        end;