2012-03-07 30 views
5

Tôi đã bắt đầu tự mình học kịch bản Innosetup. Đối với điều này tôi đã tạo ra một ứng dụng giao diện điều khiển C# đơn giản, mà đọc một phần tử từ một tệp cấu hình và kết quả đầu ra trên bàn điều khiển.cách sửa đổi exe.config từ tập lệnh Innosetup

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

Ví dụ: Đọc giá trị bằng cách truy vấn thuộc tính khóa "Tên".

Tôi muốn giá trị trong .config được viết từ tập lệnh cài đặt Innosetup.

tức Trong quá trình cài đặt tôi sẽ thu thập tên (ví dụ: "Brad Pitt" trong trường hợp này) và ghi nó vào giá trị của các tập tin cấu hình

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

Câu hỏi là làm thế nào để đạt được điều này, sử dụng tập lệnh Pascal hoặc tập lệnh chuẩn.

Bất kỳ hướng dẫn được đánh giá sâu sắc

Trân

VATSA

+0

bản sao có thể có của [Inno Setup sửa đổi tệp XML dựa trên đầu vào tùy chỉnh] (http://stackoverflow.com/questions/8141886/inno-setup-modify-xml-file-based-on-custom-input) – Deanna

Trả lời

7

Để đạt được điều này tôi đã tạo một thủ tục đơn giản, trong đó có các tên file xml như đầu vào. Thủ tục sẽ phân tích từng dòng và ghi nội dung vào một tệp tạm thời. Các mã kiểm tra mỗi dòng tìm kiếm chuỗi 'key = 'Tên'':

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

Nếu nó tìm thấy một trận đấu sau đó tôi thay thế dòng cụ thể bằng thẻ mong muốn của tôi, trong đó value được nhận từ trang tùy chỉnh của tôi .

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

Điều này được ghi vào tệp tạm thời. Sau đó tôi xóa tập tin exe.config gốc và đổi tên tập tin cấu hình tạm thời thành tập tin exe.config (do đó phản ánh những thay đổi tôi cần). Dưới đây là toàn bộ đoạn mã cho các thủ tục, và đừng quên để gọi thủ tục từ [file] tức là

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

Code Snippet

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

Tôi biết đó là một chút cũ bây giờ nhưng đây là một cách tiếp cận khác; sử dụng 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; 

Chúc mừng, Matt

+0

Sử dụng nút. Mục [i] trong mã của tôi cho tôi một "Không thể nhập VARARRAYGET" lỗi khi bắt đầu thiết lập thực tế. – Nyerguds

+0

Xin lỗi, đừng bao giờ làm điều đó. Lỗi được gây ra bởi KHÔNG sử dụng. Mục trong một lần lặp khác. Cảm ơn vì điều đó, mã của tôi hoạt động ngay bây giờ :) – Nyerguds

0

Chỉ cần đóng góp, đây sau một bản cập nhật của các thủ tục trên, bây giờ nhận được các thông số, được sử dụng với bất kỳ thuộc tính:

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; 

Cách sử dụng mẫu:

UpdateConfigKeyValue(ConfigPath,'//configuration/appSettings/add','hibernate.connection.data_source',SQLServer); 
Các vấn đề liên quan