【问题标题】:VS2015 Visual Studio Insaller=>Setup Project add custom actionVS2015 Visual Studio Installer=>Setup Project 添加自定义动作
【发布时间】:2015-11-03 07:24:38
【问题描述】:

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

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

但不知道该放在哪里以及在哪里调用这个函数。请帮帮我。

提前致谢。

【问题讨论】:

    标签: c# installation visual-studio-2015


    【解决方案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");
            }
        }
    }
    

    然后,在您的安装程序项目中,添加自定义操作(选择安装程序项目 > 右击 > 查看 > 自定义操作 > 添加自定义操作),查看应用程序文件夹(双击应用程序文件夹)添加输出(选择具有安装程序类)主要输出,然后单击确定。

    您可以在安装程序类中使用 MessageBox 进行调试。

    【讨论】:

    • 用 Visual Studio 2019 试过,效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    相关资源
    最近更新 更多