-1

I'm having some issues getting this code to function correctly. What I'm trying to accomplish is a user puts in a ConnectionString (during the setup) and its places in a already formatted file. The file is Local.config in the {app}\local directory. I was able to get it to work a few times in the {app} director but now I have nothing.

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;
Zombo
  • 1
  • 62
  • 391
  • 407

1 Answers1

-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;
  • This is far from being correct. For XML file handling you should use an XML parser. – TLama Aug 07 '15 at 08:49
  • @TLama I'm sorry you feel this way. I pulled the data from another person on this forum and modified for my needs. Please feel to offer up a better solution. – fe4rL3ssC0der Aug 07 '15 at 14:25
  • No worries. I was just replying to your claim that this is *correct*. It's not. That it works doesn't mean it is correct. [downvotes are not mine] – TLama Aug 07 '15 at 14:53