根据Inno Setup中的用户首选项编辑安装的文件 [英] Edit installed file according to user preferences in Inno Setup
问题描述
所以我已经为这个问题苦苦挣扎了几天。
当前正在为我们公司的软件安装程序,但是客户必须能够填写保存在 app.exe.config
中的URL。
我经过大量的谷歌搜索,发现这段代码是我编辑的。
var
CustomEdit:TEdit;
CustomPageID:整数;
函数LoadValueFromXML(const AFileName,APath:string):string;
var
XMLNode:变体;
XMLDocument:变体;
开始
结果:=’’;
XMLDocument:= CreateOleObject(’Msxml2.DOMDocument’);
试试
XMLDocument.async:= False;
XMLDocument.load(AFileName);
if(XMLDocument.parseError.errorCode<> 0)则
MsgBox('无法解析XML文件。'+
XMLDocument.parseError.reason,mbError,MB_OK)
else
开始
XMLDocument.setProperty('SelectionLanguage','XPath');
XMLNode:= XMLDocument.selectSingleNode(APath);
结果:= XMLNode.text;
结尾;
,除了
MsgBox(发生错误! +#13#10 + GetExceptionMessage,mbError,MB_OK);
结尾;
结尾;
过程SaveValueToXML(const AFileName,APath,AValue:string);
var
XMLNode:变体;
XMLDocument:变体;
开始
XMLDocument:= CreateOleObject(’Msxml2.DOMDocument’);
试试
XMLDocument.async:= False;
XMLDocument.load(AFileName);
if(XMLDocument.parseError.errorCode<> 0)则
MsgBox('无法解析XML文件。'+
XMLDocument.parseError.reason,mbError,MB_OK)
else
开始
XMLDocument.setProperty('SelectionLanguage','XPath');
XMLNode:= XMLDocument.selectSingleNode(APath);
XMLNode.text:= AValue;
XMLDocument.save(AFileName);
结尾;
,除了
MsgBox(发生错误! +#13#10 + GetExceptionMessage,mbError,MB_OK);
结尾;
结尾;
程序InitializeWizard;
var
CustomPage:TWizardPage;
开始
CustomPage:= CreateCustomPage(wpWelcome,自定义页面,
输入将保存到XML文件中的新值);
CustomPageID:= CustomPage.ID;
CustomEdit:= TEdit.Create(WizardForm);
CustomEdit.Parent:= CustomPage.Surface;
结尾;
过程CurPageChanged(CurPageID:Integer);
开始
如果CurPageID = CustomPageID然后
开始
CustomEdit.Text:=
LoadValueFromXML('C:\AutoScan.exe.config',
'//configuration/system.serviceModel/client/endpoint/address');
结尾;
结尾;
函数NextButtonClick(CurPageID:Integer):布尔值;
开始
结果:= True;
如果CurPageID = CustomPageID然后
开始
SaveValueToXML(
'C:\AutoScan.exe.config',
'//configuration/system.serviceModel/client /endpoint/address,CustomEdit.Text);
结尾;
结尾;
它会执行如果我指定现有路径(例如)时必须执行的操作C:\AutoScan.exe.config
,但是如果文件不存在,安装程序就会开始抱怨。
当然,该文件仅在安装后存在。但是在这种情况下,我希望在安装程序中编辑该文件,并使用 {src} \AutoScan.exe.config和 {app} \AutoScan.exe.config对其进行了尝试,但没有结果,因为安装程序开始抱怨它找不到XML文件
您可能只需要在安装完成后编辑文件即可。
程序CurStepChanged(CurStep:TSetupStep);如果CurStep = ssPostInstall,则
开始
,然后
开始
SaveValueToXML(
'C:\AutoScan.exe.config',
'// configuration / system.serviceModel / client / endpoint / address,CustomEdit.Text);
结尾;
结尾;
也不要每次都加载值进入自定义页面,因为您重置了用户首选项,所以每次用户返回到自定义页面时。
您只能在 InitializeWizard
。
可以硬编码默认值。
您确实需要从嵌入式文件中读取它,您必须暂时将其提取。
过程InitializeWizard;
var
CustomPage:TWizardPage;
开始
CustomPage:= $ b $ b CreateCustomPage(
wpWelcome,自定义页面,
输入将保存到XML文件中的新值);
CustomEdit:= TEdit.Create(WizardForm);
CustomEdit.Parent:= CustomPage.Surface;
ExtractTemporaryFile('AutoScan.exe.config');
CustomEdit.Text:=
LoadValueFromXML(
ExpandConstant('{tmp} \AutoScan.exe.config'),
'//configuration/system.serviceModel/client/端点/地址);
结尾;
So I've been struggling with this problem for a few days now.
Currently making an installer for our company software but the customer has to be able to fill in a URL that gets saved in the app.exe.config
.
I've been through a lot of googling and found this piece of code that I edited.
var
CustomEdit: TEdit;
CustomPageID: Integer;
function LoadValueFromXML(const AFileName, APath: string): string;
var
XMLNode: Variant;
XMLDocument: Variant;
begin
Result := '';
XMLDocument := CreateOleObject('Msxml2.DOMDocument');
try
XMLDocument.async := False;
XMLDocument.load(AFileName);
if (XMLDocument.parseError.errorCode <> 0) then
MsgBox('The XML file could not be parsed. ' +
XMLDocument.parseError.reason, mbError, MB_OK)
else
begin
XMLDocument.setProperty('SelectionLanguage', 'XPath');
XMLNode := XMLDocument.selectSingleNode(APath);
Result := XMLNode.text;
end;
except
MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
end;
end;
procedure SaveValueToXML(const AFileName, APath, AValue: string);
var
XMLNode: Variant;
XMLDocument: Variant;
begin
XMLDocument := CreateOleObject('Msxml2.DOMDocument');
try
XMLDocument.async := False;
XMLDocument.load(AFileName);
if (XMLDocument.parseError.errorCode <> 0) then
MsgBox('The XML file could not be parsed. ' +
XMLDocument.parseError.reason, mbError, MB_OK)
else
begin
XMLDocument.setProperty('SelectionLanguage', 'XPath');
XMLNode := XMLDocument.selectSingleNode(APath);
XMLNode.text := AValue;
XMLDocument.save(AFileName);
end;
except
MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
end;
end;
procedure InitializeWizard;
var
CustomPage: TWizardPage;
begin
CustomPage := CreateCustomPage(wpWelcome, 'Custom Page',
'Enter the new value that will be saved into the XML file');
CustomPageID := CustomPage.ID;
CustomEdit := TEdit.Create(WizardForm);
CustomEdit.Parent := CustomPage.Surface;
end;
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageID = CustomPageID then
begin
CustomEdit.Text :=
LoadValueFromXML('C:\AutoScan.exe.config',
'//configuration/system.serviceModel/client/endpoint/address');
end;
end;
function NextButtonClick(CurPageID: Integer): Boolean;
begin
Result := True;
if CurPageID = CustomPageID then
begin
SaveValueToXML(
'C:\AutoScan.exe.config',
'//configuration/system.serviceModel/client/endpoint/address', CustomEdit.Text);
end;
end;
and it does what is has to do if I specify an existing path like C:\AutoScan.exe.config
but the setup starts complaining if the file doesn't exist.
Of course the file only exists after it is installed. but in this case I want the file edited inside the installer I tried it with '{src}\AutoScan.exe.config' and '{app}\AutoScan.exe.config' but without result as the installer starts complaining it can't find the XML file
You probably just need to edit the file after the installation completes.
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssPostInstall then
begin
SaveValueToXML(
'C:\AutoScan.exe.config',
'//configuration/system.serviceModel/client/endpoint/address', CustomEdit.Text);
end;
end;
Also you should not load the value every time you get to the custom page, because you reset user preference, every time user returns back to the custom page.
You should load it only once in the InitializeWizard
.
Either hard-code the default value.
Or if you really need to read it from the embedded file, you have to temporarily extract it.
procedure InitializeWizard;
var
CustomPage: TWizardPage;
begin
CustomPage :=
CreateCustomPage(
wpWelcome, 'Custom Page',
'Enter the new value that will be saved into the XML file');
CustomEdit := TEdit.Create(WizardForm);
CustomEdit.Parent := CustomPage.Surface;
ExtractTemporaryFile('AutoScan.exe.config');
CustomEdit.Text :=
LoadValueFromXML(
ExpandConstant('{tmp}\AutoScan.exe.config'),
'//configuration/system.serviceModel/client/endpoint/address');
end;
这篇关于根据Inno Setup中的用户首选项编辑安装的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!