我想检查是否安装了主软件,如果没有安装主软件,则中止安装。检查我是否收到代码
/// <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 Insaller=>设置项目添加自定义操作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33493526/