这是我的旧实现,以获取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("-", "");
}
答案 0 :(得分:17)
这是Windows桌面的完整解决方案:
使用此代码获取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!");
}
}
}
答案 1 :(得分:8)
似乎
var deviceInformation = new EasClientDeviceInformation();
string Id = deviceInformation.Id.ToString();
正在发挥魔力,引用EasClientDeviceInformation它提供了一个唯一的ID。
Id属性表示使用从MachineID,用户SID和包系列名称的SHA256哈希的前16个字节中截断的GUID的DeviceId,其中MachineID使用本地用户组的SID。
但它仅适用于Windows应用商店应用...... 所以必须有另一种解决方案。
答案 2 :(得分:8)
有关获取ID的更好方法,请参阅this Q&A。
您需要添加对桌面和/或移动SDK的引用,以针对硬件令牌进行构建。在运行时,您应该使用ApiInformation
类型在使用之前查询API是否存在(其他设备系列,例如Xbox不具备它)。
也就是说,很多时候,当人们要求设备ID实际上并不是他们问题的最佳解决方案时 - 您是否确定需要在整个生命周期内识别物理设备,即使所有权发生变化?
答案 3 :(得分:1)
EasClientDeviceInformation不适用于Windows 10移动版。每个手机的设备ID都是相同的(我们所有的win10m客户都使用相同的GUID注册)我们需要id才能将推送消息发送到正确的手机。
答案 4 :(得分:0)
//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);
}