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

问题描述 投票:19回答:5

这是我的旧实现,用于获取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("-", "");
    }
c# windows-runtime windows-10 win-universal-app
5个回答
17
投票

这是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!");
        }
    }
}

8
投票

看起来

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

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

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

但它只适用于Windows应用商店应用......所以必须有另一种解决方案。


8
投票

Update for Windows 1609 ("Anniversary Update")

有关获取ID的更好方法,请参阅this Q&A

Old info for older OS builds

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

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


1
投票

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


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);
        }
© www.soinside.com 2019 - 2024. All rights reserved.