以 uint16 格式检索 WMI 查询信息 c#
retrieving WMI query information in uint16 format c#
我一直在尝试通过 WMI 查询检索有关我们网络中远程计算机的信息。这适用于我需要的所有信息,我只是似乎没有从 WmiMonitorID class 中获取 UserFriendlyName。由于此值存储为 uint16.
下面的代码returnsSystem.UInt16[]
但我想获得一些可读的信息。
这是我用来检索信息的方法:
GetDirectWmiQuery("UserFiendlyName", "WmiMonitorID");
public static string GetDirectWmiQuery(string item, string table)
{
string result = string.Empty;
ManagementScope scope;
scope = new ManagementScope($"\\{Var.hostnm}\root\WMI");
scope.Connect();
ObjectQuery query = new ObjectQuery($"Select {item} FROM {table}");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
result += m[item].ToString();
}
return result;
}
基于优秀的 from Jimi、
翻译成C#
The returned array needs to be converted to a string, to become human-eye-friendly. The UInt16 byte array can be converted with Convert.ToByte(UInt16), then tranformed into string with Encoding.GetString().
foreach (ManagementObject m in queryCollection)
{
var data = (ushort[])m[item];
var monitorID = Encoding.UTF8.GetString((data).Select(Convert.ToByte).ToArray());
result+= monitorID + "\n";
}
我一直在尝试通过 WMI 查询检索有关我们网络中远程计算机的信息。这适用于我需要的所有信息,我只是似乎没有从 WmiMonitorID class 中获取 UserFriendlyName。由于此值存储为 uint16.
下面的代码returnsSystem.UInt16[]
但我想获得一些可读的信息。
这是我用来检索信息的方法:
GetDirectWmiQuery("UserFiendlyName", "WmiMonitorID");
public static string GetDirectWmiQuery(string item, string table)
{
string result = string.Empty;
ManagementScope scope;
scope = new ManagementScope($"\\{Var.hostnm}\root\WMI");
scope.Connect();
ObjectQuery query = new ObjectQuery($"Select {item} FROM {table}");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
result += m[item].ToString();
}
return result;
}
基于优秀的
The returned array needs to be converted to a string, to become human-eye-friendly. The UInt16 byte array can be converted with Convert.ToByte(UInt16), then tranformed into string with Encoding.GetString().
foreach (ManagementObject m in queryCollection)
{
var data = (ushort[])m[item];
var monitorID = Encoding.UTF8.GetString((data).Select(Convert.ToByte).ToArray());
result+= monitorID + "\n";
}