2013-05-04 32 views
9

Teraz używam tego do listy wszystkich aplikacji wymienionych w rejestrze dla 32bit & 64. Widziałem inne przykłady, jak sprawdzić, czy aplikacja jest zainstalowana bez szczęścia.Sprawdź, czy aplikacja jest zainstalowana w rejestrze

string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 
RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey); 
if (key != null) 
{ 
    foreach (String a in key.GetSubKeyNames()) 
    { 
     RegistryKey subkey = key.OpenSubKey(a); 
     Console.WriteLine(subkey.GetValue("DisplayName")); 
    } 
} 

registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; 
key = Registry.LocalMachine.OpenSubKey(registryKey); 
if (key != null) 
{ 
    foreach (String a in key.GetSubKeyNames()) 
    { 
     RegistryKey subkey = key.OpenSubKey(a); 
     Console.WriteLine(subkey.GetValue("DisplayName")); 
    } 
} 

Więc ten fragment można znaleźć to wszystko w oknie konsoli i co próbuję zrobić, to tylko znaleźć jeden tytuł programu z listy nazw wyświetlanych aby sprawdzić, czy jest zainstalowany.

Ostatnią rzeczą próbowałem był

if (subkey.Name.Contains("OpenSSL")) 
    Console.Writeline("OpenSSL Found"); 
else 
    Console.Writeline("OpenSSL Not Found"); 

Wszystko starałem wróciła albo fałszywe lub fałszywie dodatni. Czy jest ktoś, kto może pokazać mi, jak wyłapać tytuł z listy?

Nie publikuj dobrze znanej funkcji prywatnej statycznej pustki IsApplicationInstalled (p_name). W ogóle mi to nie działa.

+4

Jako marginesie: Musisz 'close()' z REGKEYS już otwarty (')' 'd .. mimo to jest kod zarządzany, to są niezarządzani zasoby i będzie przeciekać jeśli ciebie nie zamykaj ich. – canhazbits

+0

Dziękuję za to FYI. – Faded

+0

To brzmi jak może być całkiem przydatne. Jednak w zależności od tego, do czego chcesz go użyć, lepiej dopasować zainstalowany kod do konkretnej aplikacji, aby działał szybciej. – Sildoreth

Odpowiedz

15

Po wyszukaniu i rozwiązywania problemów, mam go do pracy w ten sposób:

public static bool checkInstalled (string c_name) 
{ 
    string displayName; 

    string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 
    RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey); 
    if (key != null) 
    { 
     foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName))) 
     { 
      displayName = subkey.GetValue("DisplayName") as string; 
      if (displayName != null && displayName.Contains(c_name)) 
      { 
       return true; 
      } 
     } 
     key.Close(); 
    } 

    registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; 
    key = Registry.LocalMachine.OpenSubKey(registryKey); 
    if (key != null) 
    { 
     foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName))) 
     { 
      displayName = subkey.GetValue("DisplayName") as string; 
      if (displayName != null && displayName.Contains(c_name)) 
      { 
       return true; 
      } 
     } 
     key.Close(); 
    } 
    return false; 
} 

A ja po prostu zadzwoń go za pomocą

if(checkInstalled("Application Name")) 
8

Jest to czysty sposób to zrobić bez tego dużo kodu .

private static bool IsSoftwareInstalled(string softwareName) 
    { 
     var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall") ?? 
        Registry.LocalMachine.OpenSubKey(
         @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"); 

     if (key == null) 
      return false; 

     return key.GetSubKeyNames() 
      .Select(keyName => key.OpenSubKey(keyName)) 
      .Select(subkey => subkey.GetValue("DisplayName") as string) 
      .Any(displayName => displayName != null && displayName.Contains(softwareName)); 
    } 

nazywają go z if-oświadczenie:

if (IsSoftwareInstalled("OpenSSL")) 
3

Sprawdziłem kod @Stellan Lindell i to nie działa w każdym jednym przypadku. Moja wersja powinna działać ze wszystkimi scenariuszami i sprawdzić konkretną wersję zainstalowanych programów (x86, x64).

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Win32; 

namespace Test 
{ 
internal class Program 
{ 
    public enum ProgramVersion 
    { 
     x86, 
     x64 
    } 

    private static IEnumerable<string> GetRegisterSubkeys(RegistryKey registryKey) 
    { 
     return registryKey.GetSubKeyNames() 
       .Select(registryKey.OpenSubKey) 
       .Select(subkey => subkey.GetValue("DisplayName") as string); 
    } 

    private static bool CheckNode(RegistryKey registryKey, string applicationName, ProgramVersion? programVersion) 
    { 
     return GetRegisterSubkeys(registryKey).Any(displayName => displayName != null 
                    && displayName.Contains(applicationName) 
                    && displayName.Contains(programVersion.ToString())); 
    } 

    private static bool CheckApplication(string registryKey, string applicationName, ProgramVersion? programVersion) 
    { 
     RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey); 

     if (key != null) 
     { 
      if (CheckNode(key, applicationName, programVersion)) 
       return true; 

      key.Close(); 
     } 

     return false; 
    } 

    public static bool IsSoftwareInstalled(string applicationName, ProgramVersion? programVersion) 
    { 
     string[] registryKey = new [] { 
      @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", 
      @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" 
     }; 

     return registryKey.Any(key => CheckApplication(key, applicationName, programVersion)); 
    } 

    private static void Main() 
    { 
     // Examples 
     Console.WriteLine("Notepad++: " + IsSoftwareInstalled("Notepad++", null)); 
     Console.WriteLine("Notepad++(x86): " + IsSoftwareInstalled("Notepad++", ProgramVersion.x86)); 
     Console.WriteLine("Notepad++(x64): " + IsSoftwareInstalled("Notepad++", ProgramVersion.x64)); 
     Console.WriteLine("Microsoft Visual C++ 2009: " + IsSoftwareInstalled("Microsoft Visual C++ 2009", null)); 
     Console.WriteLine("Microsoft Visual C-- 2009: " + IsSoftwareInstalled("Microsoft Visual C-- 2009", null)); 
     Console.WriteLine("Microsoft Visual C++ 2013: " + IsSoftwareInstalled("Microsoft Visual C++ 2013", null)); 
     Console.WriteLine("Microsoft Visual C++ 2012 Redistributable (x86): " + IsSoftwareInstalled("Microsoft Visual C++ 2013", ProgramVersion.x86)); 
     Console.WriteLine("Microsoft Visual C++ 2012 Redistributable (x64): " + IsSoftwareInstalled("Microsoft Visual C++ 2013", ProgramVersion.x64)); 
     Console.ReadKey(); 
    } 
} 
} 
Powiązane problemy