我在安装新版本之前正在执行主要升级和卸载现有产品.但我想保留现有的配置文件.
由于早期版本没有Permanent ="yes",它会在卸载时删除配置文件.
我怎么能做这样的事情在卸载之前将'app.config'复制为'app.config.bak'.卸载后将其从"app.config.bak"恢复为"app.config".
<DirectoryRef Id="INSTALLDIR">
<Component Id="BackupConfigComponent" Guid="87368AF7-4BA2-4302-891A-B163ADDB7E9C">
<CopyFile Id="BackupConfigFile" SourceDirectory="INSTALLFOLDER" SourceName="app.config" DestinationDirectory="INSTALLFOLDER" DestinationName="app.config.bak" />
</Component>
</DirectoryRef>
<DirectoryRef Id="INSTALLDIR">
<Component Id="RestoreConfigComponent" Guid="87368AF7-4BA2-4302-891A-B163ADDB7E9C">
<CopyFile Id="RestoreConfigFile" SourceDirectory="INSTALLFOLDER" SourceName="app.config.bak" DestinationDirectory="INSTALLFOLDER" DestinationName="app.config" />
</Component>
</DirectoryRef>
<InstallExecuteSequence>
<Custom Action="BackupConfigFile" After="InstallInitialize" />
<RemoveExistingProducts After="InstallInitialize" />
<Custom Action="RestoreConfigFile" After="InstallInitialize" />
</InstallExecuteSequence>
Run Code Online (Sandbox Code Playgroud)
谢谢
您所要做的就是更改<Custom Action="RestoreConfigFile" After="InstallInitialize" />
为<Custom Action="RestoreConfigFile" After="RemoveExistingProducts " />
这只是您遇到的时间问题。您告诉所有三个操作在 InstallInitialize 之后发生,因此它们很可能不会按照写入的顺序保留。明确列出您想要的顺序始终是一个更好的主意。更好、完整的修复方法是:
<Custom Action="BackupConfigFile" After="InstallInitialize" />
<RemoveExistingProducts After="BackupConfigFile" />
<Custom Action="RestoreConfigFile" After="RemoveExistingProducts " />
Run Code Online (Sandbox Code Playgroud)
编辑:(基于评论)要在 MSI 中创建自定义操作,您需要创建一个 CustomAction 元素。还需要用于创建自定义操作的后台代码。但是,如果您只是尝试复制文件,我建议使用CopyFile元素。这比完成所有自定义操作步骤来完成我认为您要做的事情要容易和干净得多。