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

C# VS2015 Visual Studio Insaller=>;安装项目添加自定义操作,c#,installation,visual-studio-2015,C#,Installation,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 IsAppl

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

/// <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;
}
//
///检查是否安装了软件
/// 
///显示控制面板中的软件名称
已安装专用静态bool isApplication(string controlPanelDisplayName)
{
字符串显示名;
注册表键;
//搜索:CurrentUser
key=Registry.CurrentUser.OpenSubKey(@“SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”);
if(null!=键)
{
foreach(key.GetSubKeyNames()中的字符串keyName)
{
RegistryKey subkey=key.OpenSubKey(keyName);
displayName=subkey.GetValue(“displayName”)作为字符串;
if(controlPanelDisplayName.Equals(displayName,StringComparison.OrdinalIgnoreCase)==true)
{
返回true;
}
}
}
//搜索:LocalMachine_32
key=Registry.LocalMachine.OpenSubKey(@“SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”);
if(null!=键)
{
foreach(key.GetSubKeyNames()中的字符串keyName)
{
RegistryKey subkey=key.OpenSubKey(keyName);
displayName=subkey.GetValue(“displayName”)作为字符串;
if(controlPanelDisplayName.Equals(displayName,StringComparison.OrdinalIgnoreCase)==true)
{
返回true;
}
}
}
//搜索:LocalMachine_64
key=Registry.LocalMachine.OpenSubKey(@“SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall”);
if(null!=键)
{
foreach(key.GetSubKeyNames()中的字符串keyName)
{
RegistryKey subkey=key.OpenSubKey(keyName);
displayName=subkey.GetValue(“displayName”)作为字符串;
if(controlPanelDisplayName.Equals(displayName,StringComparison.OrdinalIgnoreCase)==true)
{
返回true;
}
}
}
//找不到
返回false;
}
但是不知道放在哪里,调用这个函数的地方。请帮帮我


提前感谢。

在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");
        }
    }
}
然后,在安装程序项目中,添加自定义操作(选择安装程序项目>rigth单击>查看>自定义操作>添加自定义操作),查看应用程序文件夹(双击应用程序文件夹)添加输出(选择具有安装程序类的类库)主输出,然后单击确定


您可以在installer类中使用MessageBox进行调试。

使用Visual Studio 2019进行了尝试,它工作正常。