2012-03-07 18 views
5

私はInnosetupスクリプトを自分で学習し始めました。このために、構成ファイルから要素を読み取り、コンソールに出力する単純なC#コンソールアプリケーションを作成しました。Innosetupスクリプトからexe.configを変更する方法

<configuration> 
    <appSettings> 
    <add key ="Name" value="Brad Pitt"/> 
    </appSettings> 
</configuration> 

例:キー属性「名前」を照会して値を読み取るものとします。

Innosetupセットアップスクリプトから.configファイルの値を書きたいとします。

つまり、インストールプロセスの間に私は(この場合はすなわち「ブラッド・ピット」)の名前を収集するものとし、設定ファイルの値に書き込み

<add key ="Name" value="Brad Pitt"/> 

質問は使用して、どのように私はこれを達成んですパスカルスクリプトまたは標準スクリプト。

どれガイダンスが深く、私は、入力としてXMLファイル名を取り、簡単な手順を、作成し、これを達成するために

よろしく

ヴァツサ国

+0

[Inno Setupのカスタム入力に基づいてXMLファイルを修正する] (http://stackoverflow.com/questions/8141886/inno-setup-modify-xml-file-based-on-custom-input) – Deanna

答えて

7

を高く評価しています。手順は、各行を解析し、その内容を一時ファイルに書き込む。各行は、文字列を探してコードをチェック「キー= 『名前』」:それは私がvalueは私のカスタムページから取得された私の希望タグにより、その特定の行を置き換え、その後、一致が見つかった場合

if (Pos('key="Name"', strTest) <> 0) 

strTest := ' <add key="Name" value="' + strName + '"/> '; 

これは一時ファイルに書き込まれます。私は元のexe.configファイルを削除して、exe.configファイル(したがって、私は必要な変更を反映)には、tempの設定ファイルの名前を変更します。以下は、私はそれが今、少し古いことを知っている

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

    if (FileExists(xmlFile)) then begin 
    // Load the file to a String array 
    LoadStringsFromFile(xmlFile, xmlInhalt); 

    for k:=0 to GetArrayLength(xmlInhalt)-1 do begin 
     strTest := xmlInhalt[k]; 
     if (Pos('key="Name"', strTest) <> 0) then begin 
     strTest := ' <add key="Name" value="' + strName + '"/> '; 
     end; 
     SaveStringToFile(tmpConfigFile, strTest + #13#10, True); 
    end; 

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

プロシージャのコードスニペット全体で、そしてからプロシージャを呼び出すことを忘れないでください[ファイル]すなわち

[Files] 
Source: "HUS.exe.config"; DestDir: "{app}"; AfterInstall: ConvertConfig('HUS.exe.config') 

コードスニペット別のアプローチがあります。使用MSXML

procedure UpdateConfig(); 
var 
    XMLDoc, NewNode, RootNode, Nodes, Node: Variant; 
    ConfigFilename, Key: String; 
    i: integer; 

begin 
    ConfigFilename := ExpandConstant('{app}') + '\your-app-name.exe.config'; 

    try 
     XMLDoc := CreateOleObject('MSXML2.DOMDocument'); 
    except 
    RaiseException('MSXML is required to complete the post-installation process.'#13#13'(Error ''' + GetExceptionMessage + ''' occurred)'); 
    end; 

    XMLDoc.async := False; 
    XMLDoc.resolveExternals := False; 
    XMLDoc.load(ConfigFilename); 
    if XMLDoc.parseError.errorCode <> 0 then 
    RaiseException('Error on line ' + IntToStr(XMLDoc.parseError.line) + ', position ' + IntToStr(XMLDoc.parseError.linepos) + ': ' + XMLDoc.parseError.reason); 

    RootNode := XMLDoc.documentElement; 
    Nodes := RootNode.selectNodes('//configuration/appSettings/add'); 
    for i := 0 to Nodes.length - 1 do 
    begin 
    Node := Nodes.Item[i]; 
    if Node.NodeType = 1 then 
    begin 
     key := Node.getAttribute('key'); 
     Case key of 
     'MyValue1' : Node.setAttribute('value', ConfigPage.Values[0]); 
     'MyValue2' : Node.setAttribute('value', ConfigPage.Values[1]); 
     'MyValue3' : Node.setAttribute('value', ConfigPage.Values[2]); 
     end; 
    end; 
    end; 

    XMLDoc.Save(ConfigFilename); 

end; 

乾杯、 マット

+0

私のコードでNodes.Item [i]を使用すると、 "VARARRAYGETをインポートできません"実際のセットアップを開始するとエラーが発生します。 – Nyerguds

+0

申し訳ありませんが、心配しないでください。このエラーは、別の反復で.Itemを使用しないことによって発生しています。ありがとう、私のコードは今すぐ:) – Nyerguds

0

だけで、ここでは任意の属性で使用するために、上記の手順の更新、今受信パラメータを、次の貢献:

procedure UpdateConfigKeyValue(ConfigFilename,NodeName,KeyName,Value:String); 
var 
    XMLDoc, NewNode, RootNode, Nodes, Node: Variant; 
    Key: String; 
    i: integer; 
begin 
    try 
     XMLDoc := CreateOleObject('MSXML2.DOMDocument'); 
    except 
    RaiseException('MSXML is required to complete the post-installation process.'#13#13'(Error ''' + GetExceptionMessage + ''' occurred)'); 
    end; 

    XMLDoc.async := False; 
    XMLDoc.resolveExternals := False; 
    XMLDoc.load(ConfigFilename); 
    if XMLDoc.parseError.errorCode <> 0 then 
    RaiseException('Error on line ' + IntToStr(XMLDoc.parseError.line) + ', position ' + IntToStr(XMLDoc.parseError.linepos) + ': ' + XMLDoc.parseError.reason); 

    RootNode := XMLDoc.documentElement; 
    Nodes := RootNode.selectNodes(NodeName); 
    for i := 0 to Nodes.length - 1 do 
    begin 
    Node := Nodes.Item[i]; 
    if Node.NodeType = 1 then 
    begin 
     key := Node.getAttribute('key'); 
     Case key of 
     KeyName : Node.setAttribute('value', Value); 
     end; 
    end; 
    end; 

    XMLDoc.Save(ConfigFilename); 

end; 

使用例:

UpdateConfigKeyValue(ConfigPath,'//configuration/appSettings/add','hibernate.connection.data_source',SQLServer);