C# 正在查找通过WMI读取的注册表

C# 正在查找通过WMI读取的注册表,c#,registry,wmi,C#,Registry,Wmi,我正在尝试获取我的应用程序版本,我可以使用以下代码为本地计算机执行此操作 try { string path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products"; RegistryKey key = null; if (!string.IsNullOrEmpty(path))

我正在尝试获取我的应用程序版本,我可以使用以下代码为本地计算机执行此操作

try
        {
            string path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products";

            RegistryKey key = null;

            if (!string.IsNullOrEmpty(path))
            {
                //get the 64-bit view first
                key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                key = key.OpenSubKey(path);

                //Couldn't find the value in the 64-bit view so grab the 32-bit view
                if (key == null)
                {
                    key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
                    key = key.OpenSubKey(path);
                }
            }


            foreach (string tempKeyName in key.GetSubKeyNames())
            {
                RegistryKey tempKey = key.OpenSubKey(tempKeyName + "\\InstallProperties");
                if (tempKey != null)
                {
                    if (string.Equals(Convert.ToString(tempKey.GetValue("DisplayName")), "My Application", StringComparison.CurrentCultureIgnoreCase))
                    {
                        Console.WriteLine(Convert.ToString(tempKey.GetValue("DisplayVersion")));
                        break;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
现在,我需要阅读远程机器和上面的代码段不工作,所以寻找WMI解决方案


我们可以通过WMI方式完成吗?

您可以使用WMI中的Win32_产品类。然后使用名称空间Microsoft.Management.Infrastructure用C#()远程读取。这是一种简单而好的远程读取WMI的方法。我在我的WMI程序中这样使用它:

using (var cimSession = CimSession.Create(computername))
{
    var select = $"SELECT Version FROM  Win32_Product WHERE Name = 'AppName'";

    var allVolumes = cimSession.QueryInstances(@"root\cimv2", "WQL", select);

    double value = 0;
    var cimInstances = allVolumes.ToList();
    if (!cimInstances.Any())
        throw new CimException($"No Values to read");

    foreach (var volume in cimInstances)
        value = Convert.ToDouble(volume.CimInstanceProperties["Version"].Value);
}
有。但是,WMI中存在一个错误,当您查询
Win32\u产品时,有时会导致重新配置软件,并且