【发布时间】:2021-01-26 16:35:58
【问题描述】:
我一直在尝试通过 WMI 查询检索有关我们网络中远程计算机的信息。这适用于我需要的所有信息,我只是似乎没有从 WmiMonitorID 类中获取 UserFriendlyName 。因为此值存储为 uint16。
下面的代码返回System.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;
}
【问题讨论】:
-
感谢您的建议,但该问题与 Visual Basic 中的相同问题有关。而且我不知道如何将该代码翻译成 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(). -
非常感谢,我只需要将 UInt16 字节数组转换为字节,然后将字节转换为字符串。我一定能解决这个问题!