0

我正在尝试遵循此链接上的代码,以便在我的安装程序类中确定安装程序是否以静默方式运行。但我一定是做错了什么,因为 Context.Parameters["UILevel"] 似乎不包含任何值。

在我的安装项目中,我在 Install 上添加了一个自定义操作,我将 /UILevel="[UILevel]" 传递到 CustomActionData 字段。然后,我将此自定义操作链接到安装程序 dll 项目的主要输出,其中包含以下安装程序类:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;

namespace CustomActionLibrary
{
[RunInstaller(true)]
public partial class MyInstaller : Installer
{
    string uiLevelString = string.Empty;
    public bool IsSilentInstall
    {
        get 
        {
            if (!String.IsNullOrEmpty(uiLevelString))
            {
                int level = Convert.ToInt32(uiLevelString);
                return level <= 3;
            }
            else
                return false;
        }
    }

    public MyInstaller()
    {
        InitializeComponent();
    }

    public override void Install(IDictionary stateSaver)
    {
        base.Install(stateSaver);
        uiLevelString = Context.Parameters["UILevel"];
    }

    public override void Commit(IDictionary savedState)
    {
        //System.Diagnostics.Process.Start("http://www.google.ro?q=" + uiLevelString);
        if (IsSilentInstall)
        {
            //do stuff here if it's silent install.
        }
        base.Commit(savedState);
    }
    }
 }

我认为如果我在 Install 上添加自定义操作,我应该在 Install 覆盖中检索 Context.Parameters["UILevel"]。但是 Context.Parameters["UILevel"] 永远不会被填充。我也尝试在类的构造函数上检索它,但它抛出 nullref,并且在提交事件中,但仍然没有。

我怎样才能正确检索这个值?

4

1 回答 1

0

我解决了它,它在 AfterInstall 事件处理程序上正确检索 UILevel 值。

    private void SecureUpdaterInstaller_AfterInstall(object sender, InstallEventArgs e)
    {
        uiLevelString = this.Context.Parameters["UILevel"];
        System.Diagnostics.Process.Start("http://www.google.ro?q=afterInstall_" + uiLevelString);
    }

这是有道理的——它从 Install 自定义操作中填充值,并在 AfterInstall 上检索它。

于 2011-09-23T03:58:21.263 回答