2012-01-28 59 views

回答

6

快速谷歌搜索从this page打开了下面的代码:

strComputer = "." 

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 

Set colItems = objWMIService.ExecQuery _ 
    ("Select * From Win32_Product Where Name = 'QuickTime'") 

If colItems.Count = 0 Then 
    Wscript.Echo "QuickTime is not installed on this computer." 
Else 
    For Each objItem in colItems 
     Wscript.Echo "QuickTime version: " & objItem.Version 
    Next 
End If 

“别急!”你说,“这是VBScript,而不是C#!”这是真的,但它是用于执行WMI查询的VBScript。另一个快速谷歌搜索变成了how to do WMI queries from C#

+0

聪明,确实如此。即使你不知道VBScript,它仍然是C#版本的体面伪代码。 – 2012-01-28 00:40:11

+0

谢谢,我会看看我能否得到这个工作。 – Zarxrax 2012-01-28 01:16:46

2

试试这个:

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")) 
{ 
    if (key != null) 
    { 
     foreach (string subKeyName in key.GetSubKeyNames()) 
     { 
      using (RegistryKey subKey = key.OpenSubKey(subKeyName)) 
      { 
       if (subKey == null) continue; 

       var displayName = subKey.GetValue("DisplayName") as string; 

       if (displayName == null || !displayName.Equals("QuickTime")) continue; 

       var version = subKey.GetValue("DisplayVersion"); 

       Console.WriteLine(displayName); 
       Console.WriteLine(version); 
      } 
     } 
    } 
}