VS2015 Visual Studio Insaller =>安装项目添加自定义操作

时间:2015-11-03 07:24:39

标签: c# install visual-studio-2015

我想检查是否安装了主软件,如果未安装主软件则中止设置。检查我得到的代码

/// <summary>
/// To check software installed or not
/// </summary>
/// <param name="controlPanelDisplayName">Display name of software from control panel</param>
private static bool IsApplictionInstalled(string controlPanelDisplayName)
{
    string displayName;
    RegistryKey key;

    // search in: CurrentUser
    key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
    if (null != key)
    {
        foreach (string keyName in key.GetSubKeyNames())
        {
            RegistryKey subkey = key.OpenSubKey(keyName);
            displayName = subkey.GetValue("DisplayName") as string;
            if (controlPanelDisplayName.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
            {
                return true;
            }
        }
    }

    // search in: LocalMachine_32
    key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
    if (null != key)
    {
        foreach (string keyName in key.GetSubKeyNames())
        {
            RegistryKey subkey = key.OpenSubKey(keyName);
            displayName = subkey.GetValue("DisplayName") as string;
            if (controlPanelDisplayName.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
            {
                return true;
            }
        }
    }
    // search in: LocalMachine_64
    key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
    if (null != key)
    {
        foreach (string keyName in key.GetSubKeyNames())
        {
            RegistryKey subkey = key.OpenSubKey(keyName);
            displayName = subkey.GetValue("DisplayName") as string;
            if (controlPanelDisplayName.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
            {
                return true;
            }
        }
    }
    // NOT FOUND
    return false;
}

但不知道放在哪里以及在何处调用此函数。请帮帮我。

提前感谢。

1 个答案:

答案 0 :(得分:1)

在VS2015上,您必须添加新项目(类库)。向此项目添加一个类,并从System.Configuration.Install.Installer继承。例如:

using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Windows.Forms;`

namespace InstallerAction
{
    [RunInstaller(true)]
    public partial class InstallerPathAction : Installer
    {
        //Here override methods that you need for example
        protected override void OnBeforeInstall(IDictionary savedState)
        {
            base.OnBeforeInstall(savedState);
            //Your code and here abort the installation
            throw new InstallException("No master software");
        }
    }
}

然后,在您的安装程序项目中,添加自定义操作(选择安装程序项目&gt;严格点击&gt;查看&gt;自定义操作&gt;添加自定义操作),查看应用程序文件夹(双击应用程序文件夹)添加输出(选择具有Installer类的类库)主输出,然后单击“确定”。

您可以将MessageBox用于安装程序类进行调试。