博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Windows 8 Store App】学习一:获取设备信息
阅读量:4557 次
发布时间:2019-06-08

本文共 4062 字,大约阅读时间需要 13 分钟。

通常情况下我们需要知道用户设备的一些信息:deviceId, os version, 设备制造商, 设备型号。

下面的代码用于获取设备的信息。(注:代码源于网络)

DeviceInfoHelper	public class DeviceInfoHelper	{		public async static Task
GetDeviceInfoAsync() { DeviceInfo info = new DeviceInfo(); info.DeviceID = GetDeviceID(); info.DeviceOS = (await GetWindowsVersionAsync()) + "-" + (await GetDeviceManufacturerAsync()) + "-" + (await GetDeviceModelAsync()) + "-" + (await GetDeviceCategoryAsync()); return info; } public static string GetDeviceID() { HardwareToken packageSpecificToken = HardwareIdentification.GetPackageSpecificToken(null); var hardwareId = packageSpecificToken.Id; var _internalId = ""; var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId); var array = new byte[hardwareId.Length]; dataReader.ReadBytes(array); for (var i = 0; i < array.Length; i++) { _internalId += array[i].ToString(); } return _internalId; } const string ItemNameKey = "System.ItemNameDisplay"; const string ModelNameKey = "System.Devices.ModelName"; const string ManufacturerKey = "System.Devices.Manufacturer"; const string DeviceClassKey = "{A45C254E-DF1C-4EFD-8020-67D146A850E0},10"; const string PrimaryCategoryKey = "{78C34FC8-104A-4ACA-9EA4-524D52996E57},97"; const string DeviceDriverVersionKey = "{A8B865DD-2E3D-4094-AD97-E593A70C75D6},3"; const string RootContainer = "{00000000-0000-0000-FFFF-FFFFFFFFFFFF}"; const string RootQuery = "System.Devices.ContainerId:=\"" + RootContainer + "\""; const string HalDeviceClass = "4d36e966-e325-11ce-bfc1-08002be10318"; public static async Task
GetProcessorArchitectureAsync() { var halDevice = await GetHalDevice(ItemNameKey); if (halDevice != null && halDevice.Properties[ItemNameKey] != null) { var halName = halDevice.Properties[ItemNameKey].ToString(); if (halName.Contains("x64")) return ProcessorArchitecture.X64; if (halName.Contains("ARM")) return ProcessorArchitecture.Arm; return ProcessorArchitecture.X86; } return ProcessorArchitecture.Unknown; } public static Task
GetDeviceManufacturerAsync() { return GetRootDeviceInfoAsync(ManufacturerKey); } public static Task
GetDeviceModelAsync() { return GetRootDeviceInfoAsync(ModelNameKey); } public static Task
GetDeviceCategoryAsync() { return GetRootDeviceInfoAsync(PrimaryCategoryKey); } public static async Task
GetWindowsVersionAsync() { // There is no good place to get this. // The HAL driver version number should work unless you're using a custom HAL... var hal = await GetHalDevice(DeviceDriverVersionKey); if (hal == null || !hal.Properties.ContainsKey(DeviceDriverVersionKey)) return null; var versionParts = hal.Properties[DeviceDriverVersionKey].ToString().Split('.'); return string.Join(".", versionParts.Take(2).ToArray()); } private static async Task
GetRootDeviceInfoAsync(string propertyKey) { var pnp = await PnpObject.CreateFromIdAsync(PnpObjectType.DeviceContainer, RootContainer, new[] { propertyKey }); return (string)pnp.Properties[propertyKey]; } private static async Task
GetHalDevice(params string[] properties) { var actualProperties = properties.Concat(new[] { DeviceClassKey }); var rootDevices = await PnpObject.FindAllAsync(PnpObjectType.Device, actualProperties, RootQuery); foreach (var rootDevice in rootDevices.Where(d => d.Properties != null && d.Properties.Any())) { var lastProperty = rootDevice.Properties.Last(); if (lastProperty.Value != null) if (lastProperty.Value.ToString().Equals(HalDeviceClass)) return rootDevice; } return null; } } public class DeviceInfo { public string DeviceID { get; set; } public string DeviceOS { get; set; } }

调用:

Callervar deviceInfo = await DeviceInfoHelper.GetDeviceInfoAsync();// DeviceID: 307416080***************************************************// DeviceOS: 6.2-Microsoft Corporation-Surface with Windows RT-Computer.Tablet

可以看到我的设备是一台Sruface平板。

转载于:https://www.cnblogs.com/java-koma/archive/2013/05/22/3093306.html

你可能感兴趣的文章
页面置换算法(FIFO,LRU,OPT,LRF)
查看>>
selenium+python笔记1
查看>>
Python字典 (dict)
查看>>
JavaScript:综合案例---房贷计算器的实现
查看>>
MapXtreme开发(一)
查看>>
请问乘客最终买了几等座?
查看>>
获取表行数
查看>>
less与sass的区别点
查看>>
event.keycode值大全
查看>>
array and ram
查看>>
工作笔记——禁用浏览器的返回按钮
查看>>
免费获得盛大网盘EverBox125G容量方法
查看>>
如何用spidermonkey在python里调用javascript代码
查看>>
2016级算法第一次练习赛-A.群鸦的盛宴
查看>>
浅谈深度学习和本体间的关系
查看>>
js下载文件
查看>>
python 中的高级函数filter()
查看>>
vim配置
查看>>
python创建系统时间字符串
查看>>
服务器上产看报错的日志的方法
查看>>