我使用 WixSharp 创建了一个带有 WiX 的 msi 设置。它包括几个自定义操作。例如,在安装期间,我正在执行一些正在安装和启动服务的批处理文件。在卸载过程中,它应该停止并再次卸载该服务。
var dir = new InstallDir(@"%ProgramFiles%\MyCompany\MyProduct",
new Files(@"..\..\..\AllMyFiles\*.*"));
var project = new Project("MyProduct", dir) {
GUID = new Guid("7f22db65-2b23-4df2-b2b2-495f2d369c3d"),
Version = new Version(1, 0, 0, 0),
UI = WUI.WixUI_InstallDir,
Platform = Platform.x64
};
project.Actions = new WixSharp.Action[] {
new ElevatedManagedAction(CustomActions.InstallService,Return.check, When.Before, Step.InstallFinalize, Condition.NOT_Installed),
new ElevatedManagedAction(CustomActions.StartService,Return.check, When.After, Step.PreviousAction, Condition.NOT_Installed),
new ElevatedManagedAction(CustomActions.StopService,Return.check, When.Before, Step.RemoveFiles, Condition.Installed),
new ElevatedManagedAction(CustomActions.UninstallService,Return.check, When.After, Step.PreviousAction, Condition.Installed)
};
现在到了关键部分。我需要在安装和卸载期间执行位于 INSTALLDIR 某处的批处理文件:
[CustomAction]
public static ActionResult StartService(Session session) {
string installDir = session.Property("INSTALLDIR"); //<--this works on install even when using a custom path
string workingDir = Path.Combine(installDir, @"\SomePathToTheBatchFile");
RunCmdMethode(workingDir, "something.bat -some arguments");
return ActionResult.Success;
}
[CustomAction]
public static ActionResult UninstallService(Session session) {
string installDir = session.Property("INSTALLDIR"); //<--this does not give back the right path on uninstall in case the default path was changed during installation
string workingDir = Path.Combine(installDir, @"\SomePathToTheBatchFile");
RunCmdMethode(workingDir, "something.bat -some arguments");
return ActionResult.Success;
}
使用默认安装路径时,一切运行顺利。但是,如果我在安装期间将默认安装路径更改为某个自定义路径,则安装步骤会正确找到 .bat 并执行它,但在卸载期间它会在默认文件夹中搜索 .bat 文件。尽管卸载程序正确地删除了正确位置上的文件。所以自定义安装路径必须保存在某个地方。如何正确访问它?