我正在使用实用工具逻辑的.NET Standard类库开发UWP应用。 在此应用程序中,我需要收集一些与运行PC相关的元数据
我想出了以下结构来读取数据
public static void LoadSystemInfo(this Payload payload)
{
payload.SystemInfo = new SystemInfo
{
Machine = new Machine
{
SerialNumber = SystemInfo("SerialNumber"),
UuId = SystemInfo("UuId"),
},
HostName = SystemInfo("HostName"),
OsVersion = SystemInfo("OsVersion"),
OsManufacturer = SystemInfo("OsManufacturer"),
DeviceId = SystemInfo("DeviceId"),
SystemManufacturer = SystemInfo("SystemManufacturer"),
SystemModel = SystemInfo("SystemModel"),
SystemType = SystemInfo("SystemType"),
SystemLocale = SystemInfo("SystemLocale"),
TimeZone = SystemInfo("TimeZone"),
TotalPhysicalMemory = SystemInfo("TotalPhysicalMemory"),
AvailablePhysicalMemory = SystemInfo("AvailablePhysicalMemory"),
};
}
private static string SystemInfo(string key)
{
switch (key)
{
case "SerialNumber":
return GetMotherBoardId();
case "UuId":
return "";
case "HostName":
return "";
case "OsVersion":
return "";
case "OsManufacturer":
return "";
case "DeviceId":
return "";
case "SystemManufacturer":
return "";
case "SystemModel":
return "";
case "SystemType":
return "";
case "SystemLocale":
return "";
case "TimeZone":
return "";
case "TotalPhysicalMemory":
break;
case "AvailablePhysicalMemory":
return "";
default:
return $"Missing Case for {key}";
}
return null;
}
我试图获取以下“获取主板ID”
public static string GetMotherBoardId()
{
string mbInfo = string.Empty;
ManagementScope scope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");
scope.Connect();
ManagementObject wmiClass = new ManagementObject(scope,
new ManagementPath("Win32_BaseBoard.Tag=\"Base Board\""), new ObjectGetOptions());
foreach (PropertyData propData in wmiClass.Properties)
{
if (propData.Name == "SerialNumber")
mbInfo = $"{propData.Name,-25}{Convert.ToString(propData.Value)}";
}
return mbInfo;
}
这会引发错误,因为系统。当前仅Windows桌面应用程序支持管理。
如何从运行我的UWP应用的本地PC获取上述所有属性。
还尝试了如下所示的powershell脚本
using (PowerShell powerShellInstance = PowerShell.Create())
{
powerShellInstance.AddCommand("get-wmiobject");
powerShellInstance.AddParameter("class", "Win32_ComputerSystemProduct");
//powerShellInstance.AddScript(
// "get-wmiobject Win32_ComputerSystemProduct | Select-Object -ExpandProperty UUID");
Collection<PSObject> psOutput = powerShellInstance.Invoke();
}
出现以下错误
“ get-wmiobject”一词无法识别为cmdlet的名称, 功能,脚本文件或可操作程序。检查拼写 名称,或者如果包含路径,请确认路径正确,并 再试一次。
更新
遵循本文https://docs.microsoft.com/en-us/windows/desktop/wmisdk/retrieving-an-instance,但仍然失败
string Namespace = @"root\cimv2";
string className = "Win32_LogicalDisk";
CimInstance myDrive = new CimInstance(className, Namespace);
CimSession mySession = CimSession.Create("localhost");
CimInstance searchInstance = mySession.GetInstance(Namespace, myDrive);
引发以下错误
客户端无法访问CIM资源。
当我尝试
ManagementObjectSearcher mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
ManagementObjectCollection colDisks = mgmtObjSearcher.Get();
我收到此错误
目前仅Windows桌面支持System.Management 应用程序。
当我尝试这个
string command = "Get-Command Write-Output";
using (var ps = PowerShell.Create())
{
var results = ps.AddScript(command).Invoke();
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}
ps.Commands.Clear();
}
我收到此错误
创建管道时发生错误。 ->找不到方法: 'System.Text.StringBuilder System.Text.StringBuilder.Append(System.Text.StringBuilder)'。
我非常感谢您的帮助。
答案 0 :(得分:0)
我根据Microsoft工具包SystemInformation
帮助程序中的数据,NetworkInformation
类的主机名,TimeZone.CurrentTimeZone
的时区和HomeGeographicRegion
的SystemLocale填充了您的开关
我没有填写TotalPhysicalMemory,因为在UWP中您无法获取系统RAM信息,因为它是沙箱。但是您可以从MemoryManager
对于SerialNumber
,您可以使用SystemIdentification.GetSystemIdForPublisher()
接收原始数据作为缓冲区,您可以对其进行处理。
private static string SystemInfo(string key)
{
EasClientDeviceInformation deviceInfo = new EasClientDeviceInformation();
switch (key)
{
case "SerialNumber":
break ;
case "UuId":
return deviceInfo.Id.ToString();
case "HostName":
var hostNames = NetworkInformation.GetHostNames();
return hostNames.FirstOrDefault(name => name.Type == HostNameType.DomainName)?.DisplayName ?? "???";
case "OsVersion":
ulong version = ulong.Parse(AnalyticsInfo.VersionInfo.DeviceFamilyVersion);
return ((version & 0xFFFF000000000000L) >> 48).ToString()+"."+
((version & 0x0000FFFF00000000L) >> 32).ToString()+"."+((version & 0x00000000FFFF0000L) >> 16).ToString()+"."+(version & 0x000000000000FFFFL).ToString();
case "OsManufacturer":
return deviceInfo.OperatingSystem;
case "DeviceId":
return "";
case "SystemManufacturer":
return deviceInfo.SystemManufacturer;
case "SystemModel":
return deviceInfo.SystemProductName;
case "SystemType":
return Package.Current.Id.Architecture.ToString();
case "SystemLocale":
return Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion;
case "TimeZone":
return TimeZone.CurrentTimeZone.StandardName.ToString();
case "TotalPhysicalMemory":
break;
case "AvailablePhysicalMemory":
return ((float)MemoryManager.AppMemoryUsageLimit / 1024 / 1024).ToString();
default:
return $"Missing Case for {key}";
}
return null;
}