来自通用 Windows 平台或 .NET Standard 2.0 class 库的系统信息

System Info from Universal Windows Platform or .NET Standard 2.0 class Library

我正在使用 .NET Standard class 实用程序逻辑库开发 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;
        }

这会引发错误,因为 System.Management 目前仅支持 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();

                    }

给出如下错误

The term 'get-wmiobject' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

更新


已关注这篇文章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);

抛出以下错误

Access to a CIM resource was not available to the client.

当我尝试这个时

  ManagementObjectSearcher mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
                    ManagementObjectCollection colDisks = mgmtObjSearcher.Get();

我收到这个错误

System.Management currently is only supported for Windows desktop applications.

当我尝试这个时

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();
                    }

我收到这个错误

An error occurred while creating the pipeline. --> Method not found: 'System.Text.StringBuilder System.Text.StringBuilder.Append(System.Text.StringBuilder)'.

非常感谢任何帮助。

我根据 Microsoft 工具包中的数据填写了您的开关 SystemInformation helper, hostname from NetworkInformation class, timezone from TimeZone.CurrentTimeZone and SystemLocale from HomeGeographicRegion

我没有填充 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;
}