【问题标题】:Do some action before application install in C#在 C# 中安装应用程序之前执行一些操作
【发布时间】:2014-11-01 11:29:07
【问题描述】:

我正在使用 C# 和 .NET 4.0 框架来开发 WPF 应用程序。我需要执行这样的任务:在桌面上安装我的 WPF 应用程序之前,我需要删除该安装桌面中的一些文件夹。我尝试了以下一个:

文件安装程序.cs

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
        }
    }
}

【问题讨论】:

  • 作为一个建议:使用像 WIX(免费)这样的框架来制作你的安装程序。安装某些东西是一项复杂的任务,尤其是在发生意外情况时。 WiX 需要一些时间来理解,但是非常强大
  • 您使用什么来安装应用程序? InstallShield, Wix, ClickOnce, xcopy, ... ??
  • @Mrchief 我正在使用 VS2010 构建解决方案。MSI 文件已生成,我正在使用该 msi 文件进行安装。
  • 您有机会发布完整的installer.cs 代码吗?
  • 我认为您最好的选择是使用纯 msi 解决方案。有一个 removefile 表。使用 orca 执行此操作,请参阅 symantec.com/connect/forums/…

标签: c# .net visual-studio-2010 c#-4.0


【解决方案1】:

删除目标系统上的文件/文件夹是危险的,应该避免。您应该只删除 创建的文件;你是你的 MSI 文件。既然您已收到警告,以下是您的选择:

  • 使用 Visual Studio 安装项目,这是不可能的,或者至少不是微不足道的。您需要使用 C++ 实现自定义操作,编译成 DLL,构建您的 MSI,change the install sequence using OrcaCostFinalize 之前运行。

  • 使用第三方工具为安装程序项目提供更多功能,例如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>
    

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
相关资源
最近更新 更多