如何在Windows 10 Universal中获取设备的唯一标识符?

这是我的旧实现,用于获取Windows Universal 8.1的唯一DeviceID,但类型HardwareIdentification不再存在。

private static string GetId() { var token = HardwareIdentification.GetPackageSpecificToken(null); var hardwareId = token.Id; var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId); byte[] bytes = new byte[hardwareId.Length]; dataReader.ReadBytes(bytes); return BitConverter.ToString(bytes).Replace("-", ""); } 

这是Windows桌面的完整解决方案:

  • 添加扩展引用“UWP的Windows桌面扩展”,如Peter Torr – MSFT提到的那样。

使用此代码获取HardwareId:

 using System; using Windows.Security.ExchangeActiveSyncProvisioning; using Windows.System.Profile; namespace Tobit.Software.Device { public sealed class DeviceInfo { private static DeviceInfo _Instance; public static DeviceInfo Instance { get { if (_Instance == null) _Instance = new DeviceInfo(); return _Instance; } } public string Id { get; private set; } public string Model { get; private set; } public string Manufracturer { get; private set; } public string Name { get; private set; } public static string OSName { get; set; } private DeviceInfo() { Id = GetId(); var deviceInformation = new EasClientDeviceInformation(); Model = deviceInformation.SystemProductName; Manufracturer = deviceInformation.SystemManufacturer; Name = deviceInformation.FriendlyName; OSName = deviceInformation.OperatingSystem; } private static string GetId() { if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.System.Profile.HardwareIdentification")) { var token = HardwareIdentification.GetPackageSpecificToken(null); var hardwareId = token.Id; var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId); byte[] bytes = new byte[hardwareId.Length]; dataReader.ReadBytes(bytes); return BitConverter.ToString(bytes).Replace("-", ""); } throw new Exception("NO API FOR DEVICE ID PRESENT!"); } } } 

看起来

 var deviceInformation = new EasClientDeviceInformation(); string Id = deviceInformation.Id.ToString(); 

正在做的魔术, 引用EasClientDeviceInformation它提供了一个独特的Id。

Id属性表示使用从MachineID,用户SID和包族名称的SHA256哈希的前16个字节截断的GUID的DeviceId,其中MachineID使用本地用户组的SID。

但它只适用于Windowsapp store应用……所以必须有另一种解决方案。

Windows 1609更新(“周年纪念更新”)

有关获取ID的更好方法,请参阅此问答 。

旧操作系统版本的旧信息

您需要添加对桌面和/或移动SDK的引用,以针对硬件令牌进行构建。 在运行时,您应该使用ApiInformation类型在使用它之前查询API是否存在(其他设备系列如Xbox没有它)。

也就是说,很多时候人们要求的设备ID实际上并不是他们问题的最佳解决方案 – 您是否确定需要在整个生命周期内识别物理设备,即使所有权发生变化?

EasClientDeviceInformation不适用于Windows 10移动版。 每个电话的设备ID都是相同的(我们所有的win10m客户都使用相同的GUID注册)我们需要id才能将推送消息发送到正确的手机。

 //you can use this //its working with me very fine on windows 10 //replace the word bios with any hardware name you want //data also can be found with using windows application named (wbemtest) using System.Management; public static async Task ReturnHardWareID() { string s = ""; Task task = Task.Run(() => { ManagementObjectSearcher bios = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS"); ManagementObjectCollection bios_Collection = bios.Get(); foreach (ManagementObject obj in bios_Collection) { s = obj["SerialNumber"].ToString(); break; //break just to get the first found object data only } }); Task.WaitAll(task); return await Task.FromResult(s); }