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

标签 c# windows-runtime windows-10 win-universal-app

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

关于c# - 如何在 Windows 10 Universal 中获取设备的唯一标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57559688/

相关文章:

c# - FontWeight 属性不起作用 [Windows Phone 8.1 运行时]

.net - 如何在 Windows 10 上安装 OpenXML SDK 生产力工具 2.5

c# - 在 asp.net mvc razor 中使用 JQuery 范围 slider

c# - 实现自定义 WPF 命令

c# - 将对象 "fixed"保持在镜头前(Google Cardboard + Unity)

textbox - UWP/WinRT : How to save and then restore a simple TextBox for resume from termination?

javascript - WinRT - Javascript - 是否可以实现 FTP、SSH、SFTP 等?

debian - 无法在 Windows 10 上安装 BeagleBone 的驱动程序

node.js - 使用 node.js 安装 xml2json 时出错

c# - 在单独的 js 文件中访问 Web 配置