这是我的旧的实现得到一个独特的DeviceID为Windows通用8.1,但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桌面的完整解决方案:
- 添加扩展基准“Windows桌面扩展为UWP”像彼得·托 - MSFT提及。
使用此代码来获取硬件ID:
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属性代表使用GUID设备ID从第一16个字节,其中机号使用本地用户组的SID的机号,用户SID,和套餐家庭名称的SHA256散列的截断。
但它仅适用于Windows应用商店的应用程序...所以必须是另一种解决方案。
更新的Windows 1609(“周年更新”)
见这个Q&A为一个更好的方式来获得一个ID。
老年人OS旧信息构建
您需要将引用添加到桌面和/或移动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<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);
}