gpt4 book ai didi

c# - VS2015 Visual Studio Installer=>安装项目添加自定义操作

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:14 25 4
gpt4 key购买 nike

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

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

但不知道该放在哪里,该函数在哪里调用。请帮助我。

提前致谢。

最佳答案

在 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 进行调试。

关于c# - VS2015 Visual Studio Installer=>安装项目添加自定义操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33493526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com