2015-11-03 131 views
2

我想檢查主軟件是否安裝,如果主軟件沒有安裝,然後中止安裝。檢查我得到的代碼VS2015 Visual Studio Insaller =>設置項目添加自定義動作

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

在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進入安裝程序類進行調試。

相關問題