在C#中安装应用程序之前,请执行一些操作
本文关键字:执行 操作 安装 应用程序 | 更新日期: 2023-09-27 18:28:53
我正在使用C#和.NET 4.0框架来开发WPF应用程序。我需要执行这样的任务:在桌面上安装我的WPF应用程序之前,我需要删除安装桌面中的一些文件夹。我在中尝试了以下内容
文件安装程序
this.BeforeInstall +=new InstallEventHandler(Installer_BeforeInstall);
void Installer_BeforeInstall(object sender, InstallEventArgs e)
{
// Code
}
无论我在上面的方法中使用了什么代码来删除文件夹/目录,都是在我的WPF应用程序安装后执行的,这意味着安装完成了一半。在WPF应用程序将安装文件夹保存在桌面上之前,我需要删除该文件夹。如何使用.NET 4.0在C#中实现这一点?
更新:installer.cs
namespace namespace name
{
[RunInstaller(true)]
public partial class Installer : System.Configuration.Install.Installer
{
public Installer()
{
InitializeComponent();
this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
this.BeforeUninstall += new InstallEventHandler(ServiceInstaller_BeforeUninstall);
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
}
void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
//code
}
void ServiceInstaller_BeforeUninstall(object sender, InstallEventArgs e)
{
// Code
}
}
}
删除目标系统上的文件/文件夹是危险的,应该避免。您应该只删除您创建的文件;你就是你的MSI文件。既然你已经收到警告,下面是你的选择:
-
使用Visual Studio安装程序项目,这是不可能的,或者至少不是微不足道的。您需要使用C++实现自定义操作,编译成DLL,构建MSI,使用Orca更改安装顺序以在
CostFinalize
之前运行。 -
使用第三方工具为安装程序项目提供更多功能,如AdvancedInstaller
-
如果你做决定去第三方,我会说使用WiX。看看RemoveFolder操作,或者你可以使用这样的
CustomAction
:<CustomAction Id="DeleteFiles" Return="check" Value="del path'to'folder" /> <InstallExecuteSequence> <Custom Action="DeleteFiles" After='InstallFinalize'>Installed AND NOT UPGRADINGPRODUCTCODE</Custom> ... </InstallExecuteSequence>