[英]looking for registry read through WMI
我正在嘗試獲取我的應用程序版本,並且能夠使用以下代碼在本地計算機上運行,
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_Product類。 然后使用命名空間Microsoft.Management.Infrastructure通過C#遠程讀取( https://docs.microsoft.com/zh-cn/windows/desktop/wmisdk/connecting-to-wmi-on-a-remote-computer ) 。 這是一種遠程讀取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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.