【Windows 8 Store App】学习一:获取设备信息

原文http://www.cnblogs.com/java-koma/archive/2013/05/22/3093306.html

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

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

	public class DeviceInfoHelper
	{
		public async static Task<DeviceInfo> 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<ProcessorArchitecture> 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<string> GetDeviceManufacturerAsync()
		{
			return GetRootDeviceInfoAsync(ManufacturerKey);
		}

		public static Task<string> GetDeviceModelAsync()
		{
			return GetRootDeviceInfoAsync(ModelNameKey);
		}

		public static Task<string> GetDeviceCategoryAsync()
		{
			return GetRootDeviceInfoAsync(PrimaryCategoryKey);
		}

		public static async Task<string> 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<string> GetRootDeviceInfoAsync(string propertyKey)
		{
			var pnp = await PnpObject.CreateFromIdAsync(PnpObjectType.DeviceContainer,
					  RootContainer, new[] { propertyKey });
			return (string)pnp.Properties[propertyKey];
		}

		private static async Task<PnpObject> 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; }
	}

 

调用:

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

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

时间: 2024-08-03 15:35:56

【Windows 8 Store App】学习一:获取设备信息的相关文章

重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化

原文:重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 [源码下载] 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通信 获取网络信息 序列化 - json 序列化 - xml 序列化 - rss atom 示例1.演示如何获取网络的相关信息Communication/NetworkInfo.xa

Windows 8 Store Apps学习(60) 通信: 获取网络信息, 序列化和反序列化

介绍 重新想象 Windows 8 Store Apps 之 通信 获取网络信息 序列化 - json 序列化 - xml 序列化 - rss atom 示例 1.演示如何获取网络的相关信息 Communication/NetworkInfo.xaml.cs /* * 演示如何获取网络的相关信息 */ using System; using System.Collections.Generic; using Windows.Networking.Connectivity; using Windo

【Windows 8 Store App】学习:目录

原文http://www.cnblogs.com/java-koma/archive/2013/05/22/3093302.html 写在前面:我之前从事java开发,对MS的一整套东西还没入门哈,难免有写错的地方,欢迎大家指出我的错误.   简 介:据说MS想通过XAML一统手机/平板/PC开发,所以只要你学会了其中一种平台的开发,另外1个平台就会驾轻就熟,因为很多控件是共用的.这里要稍 微说明一下:Windows 8平板和PC的开发是一样的,发布到同一个Store,如果你开发了一款Windo

Windows phone Store APP如何设置页面横竖屏

在 windows phone store app 中,判断和设置页面横竖屏的方法,与 silverlight 中的 Page 类 不同,不能直接通过 Page.Orientation 进行设置.而是通过 DisplayInformation 类,方法如下: // 横屏 Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences = Windows.Graphics.Display.DisplayOrientations

微信小程序把玩(三十八)获取设备信息 API

原文:微信小程序把玩(三十八)获取设备信息 API 获取设备信息这里分为四种, 主要属性: 网络信息wx.getNetWorkType, 系统信息wx.getSystemInfo, 重力感应数据wx.onAccelerometerChange, 罗盘数据wx.onCompassChange wxml <button type="primary" bindtap="getNetWorkType">获取网络类型</button> <butt

Windows 8 Store Apps学习(49) 输入: 获取输入设备信息

输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 介绍 重新想象 Windows 8 Store Apps 之 输入 输入设备的相关信息 SIP(Soft Input Panel)的应用 Tab 键导航 Pointer - 指针,鼠标 Tap - 触摸 Drag 和 Drop 示例 1.演示如何获取输入设备的相关信息 Input/InputDeviceInfo.xaml <Page x:Class="XamlDemo.Input.In

Windows 8 Store Apps学习(41) 打印

介绍 重新想象 Windows 8 Store Apps 之 打印 示例 1.需要打印的文档 Print/PrintPage.xaml <Page x:Class="XamlDemo.Print.PrintPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xam

Windows 8 Store Apps学习(30) 信息

信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息 介绍 重新想象 Windows 8 Store Apps 之 信息 获取包信息 获取系统信息 获取硬件信息 获取即插即用(PnP: Plug and Play)的设备的信息 获取常用设备信息 示例 1.演示如何获取 app 的 package 信息 Information/PackageInfo.xaml.cs /* * 演示如何获取 app 的 package 信息 */ using System; using Wind

Windows 8 Store Apps学习(21) 动画: ThemeTransition(过渡效果)

介绍 重新想象 Windows 8 Store Apps 之 动画 ThemeTransition 的概述 EntranceThemeTransition - 页面间跳转时的过渡效果 ContentThemeTransition - 内容改变时的过渡效果 RepositionThemeTransition - 位置改变时的过渡效果 PopupThemeTransition - 弹出时的过渡效果 AddDeleteThemeTransition - 添加项或删除项时的过渡效果 ReorderThe