Inno Setup运行setup.exe时修改app.config文件

时间:2013-03-25 14:47:08

标签: .net wcf web-services c#-4.0 inno-setup

我有一个WCF服务,我作为Windows服务托管。我通常会去VS命令提示符并使用installutil.exe安装服务,然后根据我安装它的机器名修改app.config中服务的基地址并运行服务。

基地址如下:

<endpoint address="http://MACHINE_NAME/NFCReader/" binding="webHttpBinding"/>

我修改了app.config文件中的MACHINE_NAME。

我想使用inno设置为我做同样的事情。

我想要的是当用户运行setup.exe文件来安装服务时,我想提示用户提供服务的基地址并使用该地址来托管它。我无法弄清楚它是否可能或如何做到。

请帮忙吗?提前致谢。 :)

2 个答案:

答案 0 :(得分:5)

我只是用来替换app app中的字符串的例子 我相信它可以做得更好: - )

我所取代的是:

add key =“AppVersion”value =“YYMMDD.HH.MM”

[Code]
procedure Update;
var
C: AnsiString;
CU: String;
begin
        LoadStringFromFile(ExpandConstant('{src}\CdpDownloader.exe_base.config'), C);
        CU := C;
        StringChange(CU, 'YYMMDD.HH.MM', GetDateTimeString('yymmdd/hh:nn', '.', '.'));
        C := CU;
        SaveStringToFile(ExpandConstant('{src}\Config\CdpDownloader.exe.config'), C, False);          
end;

function InitializeSetup: Boolean;
begin
  Update;
result := True;
end;

答案 1 :(得分:2)

我建议您使用XML解析器来更新配置文件。以下功能可以帮助您。它使用MSXML作为文件解析器:

[Code]
const
  ConfigEndpointPath = '//configuration/system.serviceModel/client/endpoint';

function ChangeEndpointAddress(const FileName, Address: string): Boolean;
var
  XMLNode: Variant;
  XMLDocument: Variant;  
begin
  Result := False;
  XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
  try
    XMLDocument.async := False;
    XMLDocument.preserveWhiteSpace := True;
    XMLDocument.load(FileName);    
    if (XMLDocument.parseError.errorCode <> 0) then
      RaiseException(XMLDocument.parseError.reason)
    else
    begin
      XMLDocument.setProperty('SelectionLanguage', 'XPath');
      XMLNode := XMLDocument.selectSingleNode(ConfigEndpointPath);
      XMLNode.setAttribute('address', Address);
      XMLDocument.save(FileName);
      Result := True;
    end;
  except
    MsgBox('An error occured during processing application ' +
      'config file!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
  end;
end;
相关问题