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

简介: 原文 http://www.cnblogs.com/java-koma/archive/2013/05/22/3093306.html 通常情况下我们需要知道用户设备的一些信息:deviceId, os version, 设备制造商, 设备型号。

原文 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平板。

目录
相关文章
|
2月前
|
运维 iOS开发 Windows
windows电脑备案ios APP获取公钥和证书指纹Sha-1值的方法
在阿里云进行APP备案、在备案IOS端的环节的时候,发现需要我们将p12证书安装在电脑上,再用xcode或或钥匙串访问来获取这个证书的公钥和sha-1值。 但是大部分开发uniapp应用的同学们,或者进行发布的运维人员的电脑都是windows,无法按照阿里云的教程来获取ios的公钥和sha-1。备案就被卡主了。 这里介绍下另一个方法,就是使用香蕉云编来在线上传证书获取。如下图所示,打开香蕉云编后,找到下图这个功能
537 0
|
10月前
|
Dart 前端开发 架构师
【01】vs-code如何配置flutter环境-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈-供大大的学习提升
【01】vs-code如何配置flutter环境-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈-供大大的学习提升
533 26
|
C# Windows
【Azure App Service】在App Service for Windows上验证能占用的内存最大值
根据以上测验,当使用App Service内存没有达到预期的值,且应用异常日志出现OutOfMemory时,就需要检查Platform的设置是否位64bit。
203 11
|
小程序 前端开发 Java
SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
JavaDog Chat v1.0.0 是一款基于 SpringBoot、MybatisPlus 和 uniapp 的简易聊天软件,兼容 H5、小程序和 APP,提供丰富的注释和简洁代码,适合初学者。主要功能包括登录注册、消息发送、好友管理及群组交流。
344 0
SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
|
API 开发工具 UED
在 UWP 中使用 Windows App SDK
【10月更文挑战第17天】在UWP中使用Windows App SDK可增强应用功能和性能。首先了解SDK特性,接着安装Visual Studio 2022及以上版本,并从微软官网下载安装SDK。配置项目时,确保目标版本支持SDK,添加SDK引用后即可使用新API提升应用体验。开发过程中应充分利用调试工具进行测试,确保应用的兼容性和稳定性。
276 0
|
Java 应用服务中间件 开发工具
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
169 2
|
Java 应用服务中间件 Windows
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
138 2
|
PHP Windows
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
260 1

热门文章

最新文章