如何在 Windows 10 Universal 中获取设备的唯一标识符?
How do I get a Unique Identifier for a Device within Windows 10 Universal?
这是我为 Windows Universal 8.1 获取唯一设备 ID 的旧实现,但 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("-", "");
}
好像
var deviceInformation = new EasClientDeviceInformation();
string Id = deviceInformation.Id.ToString();
正在施展魔法,参考 EasClientDeviceInformation 它提供了一个唯一的 ID。
The Id property represents the DeviceId using the GUID truncated from the first 16 bytes of the SHA256 hash of the MachineID, User SID, and Package Family Name where the MachineID uses the SID of the local users group.
但它只适用于 Windows 商店应用程序... 所以必须有另一种解决方案。
更新 Windows 1609 ("Anniversary Update")
有关获取 ID 的更好方法,请参阅 。
旧 OS 版本的旧信息
您需要添加对桌面和/或移动 SDK 的引用以针对硬件令牌进行构建。在运行时,您应该使用 ApiInformation
类型来查询 API 是否存在,然后再使用它(其他设备系列如 Xbox 没有)。
也就是说,很多时候当人们要求提供设备 ID 时,这实际上并不是解决他们问题的最佳方法 -- 您确定您需要在整个生命周期内识别物理设备,即使所有权发生变化也是如此?
这是 Windows 桌面的完整解决方案:
- 添加扩展参考 "Windows Desktop Extensions for the UWP" 就像提到的 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!");
}
}
}
EasClientDeviceInformation 不适用于 Windows 10 手机。每个 phone 的设备 ID 都相同(我们所有的 win10m 客户都使用相同的 GUID 注册)我们需要该 ID 才能向右侧发送推送消息 phone。
//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<string> 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);
}
这是我为 Windows Universal 8.1 获取唯一设备 ID 的旧实现,但 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("-", "");
}
好像
var deviceInformation = new EasClientDeviceInformation();
string Id = deviceInformation.Id.ToString();
正在施展魔法,参考 EasClientDeviceInformation 它提供了一个唯一的 ID。
The Id property represents the DeviceId using the GUID truncated from the first 16 bytes of the SHA256 hash of the MachineID, User SID, and Package Family Name where the MachineID uses the SID of the local users group.
但它只适用于 Windows 商店应用程序... 所以必须有另一种解决方案。
更新 Windows 1609 ("Anniversary Update")
有关获取 ID 的更好方法,请参阅
旧 OS 版本的旧信息
您需要添加对桌面和/或移动 SDK 的引用以针对硬件令牌进行构建。在运行时,您应该使用 ApiInformation
类型来查询 API 是否存在,然后再使用它(其他设备系列如 Xbox 没有)。
也就是说,很多时候当人们要求提供设备 ID 时,这实际上并不是解决他们问题的最佳方法 -- 您确定您需要在整个生命周期内识别物理设备,即使所有权发生变化也是如此?
这是 Windows 桌面的完整解决方案:
- 添加扩展参考 "Windows Desktop Extensions for the UWP" 就像提到的 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!");
}
}
}
EasClientDeviceInformation 不适用于 Windows 10 手机。每个 phone 的设备 ID 都相同(我们所有的 win10m 客户都使用相同的 GUID 注册)我们需要该 ID 才能向右侧发送推送消息 phone。
//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<string> 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);
}