C# 检查是否存在microsoft组件

C# 检查是否存在microsoft组件,c#,C#,我想检查某些microsoft组件,如wmencoder、directx或wmplayer 是否已安装。如果已安装,我还可以获取其版本号吗 我该怎么做 提前谢谢。我的第一个想法是WMI。在上初始化Win32\u SoftwareElement 但是可能需要一些工作才能得到正确的类和查询。从WMI CIM Studio的WMI工具开始 使用PowerShell,类似于: gwmi win32_softwareelement -filter "name like '%play%'" | ft 将允

我想检查某些microsoft组件,如wmencoder、directx或wmplayer 是否已安装。如果已安装,我还可以获取其版本号吗

我该怎么做


提前谢谢。

我的第一个想法是WMI。在上初始化Win32\u SoftwareElement

但是可能需要一些工作才能得到正确的类和查询。从WMI CIM Studio的WMI工具开始

使用PowerShell,类似于:

gwmi win32_softwareelement -filter "name like '%play%'" | ft
将允许查找正确的ID。警告:这是非常缓慢的


MS Installer MSI API可能有更快的功能。

我使用RegShot来确定可用于检查是否安装了softwre的注册表设置

我使用下面的命令来确定是否安装了其他应用程序,但是,您需要从Visual Studio中的安装项目中了解应用程序在注册表中安装时使用的唯一产品代码

包括

using System.Diagnostics;
using Microsoft.Win32; 
用法:

// HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{0006F03A-0000-0000-C000-000000000046} << This is outlook 2003
String retval = "";

// Look to see if Outlook 2003 is installed and if it is...
if ((checkComServerExists("{0006F03A-0000-0000-C000-000000000046}", out retval)))
{
    // Update boolean flag if we get this far so we don't have to check again
    Console.WriteLine("Office CSLID exists - Version: " + retval);
}

除其他外,它使用和注册表访问。

这是用于安装程序的吗?或者你需要在你的C程序代码中检查这个问题吗?根据问号,这似乎是一个C问题。您会为PowerShell脚本发布一个与.NET相当的C代码吗?谢谢。这会花很多时间。我不经常在.NET代码中使用WMI,而且我可能没有时间。要放入System.management查询类的WQL:从Win32_SoftwareElement中选择*,其中的名称类似于“%play%”。
// Checks to see if the given CLSID is registerd and exists on the system
private static Boolean checkComServerExists(String CLSID, out String retval)
{
    RegistryKey myRegKey = Registry.LocalMachine;
    Object val;

    try
    {
        // get the pathname to the COM server DLL/EXE if the key exists
        myRegKey = myRegKey.OpenSubKey("SOFTWARE\\Classes\\CLSID\\" + CLSID + "\\LocalServer32");
        val = myRegKey.GetValue(null); // the null gets default
    }
    catch
    {
        retval = "CLSID not registered";
        return false;
    }

    FileVersionInfo myFileVersionInfo = null;
    try
    {
        // parse out the version number embedded in the resource
        // in the DLL
        myFileVersionInfo = FileVersionInfo.GetVersionInfo(val.ToString());
    }
    catch
    {
        retval = String.Format("DLL {0} not found", val.ToString());
        return false;
    }

    retval = myFileVersionInfo.FileVersion;
    return true;
}