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

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

  1. 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop

    [源码下载] 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 作者:weba ...

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

    [源码下载] 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  3. 【Windows 8 Store App】学习:目录

    原文http://www.cnblogs.com/java-koma/archive/2013/05/22/3093302.html 写在前面:我之前从事java开发,对MS的一整套东西还没入门哈,难 ...

  4. 小白学phoneGap《构建跨平台APP:phoneGap移动应用实战》连载五(使用PhoneGap获取设备信息)

    除了能够将HTML页面打包成可以直接安装运行的APP外,PhoneGap的一个最大优势在于可以通过JavaScript调用设备来访问设备上的硬件信息,从而实现一些原本只有依靠原生SDK才能够达到的目的 ...

  5. React Native(五)——获取设备信息react-native-device-info

    心酸史: 自从接触rn开始后,越来越多的引入第三方组件而开始的配置文件,让自己一再头疼: 明明是按照官方文档一步一步的配置,为什么别人可以做到的自己却屡屡出错,真是哭笑不得--从微信分享react-n ...

  6. appium自动化测试框架——封装获取设备信息类

    在上一节中,我们已经解决了如何在python中执行cmd,并获取执行结果.下面就小小实战一下,获取设备信息. 一.思路 1.windows上获取设备信息的方法 输入dos命令“adb devices” ...

  7. Android 获取设备信息 异常

    /**获取设备信息 * @param c * @return */ public static void setDeviceInfo(Context c,RequestParams params){ ...

  8. 微信小程序 --- 获取设备信息

    获取设备信息: wx.getSystemInfo model:手机型号 pixelRatio:设备像素比 windowWidth:窗口宽度 windowHeight:窗口高度 language:语言 ...

  9. PhoneGap获取设备信息

    一. 获取设备信息的方法列表(如果没有或者检测不出来就显示undefined) 1.device.name              设备名称(一些国产机检测不出来) 2.device.model   ...

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

    原文:微信小程序把玩(三十八)获取设备信息 API 获取设备信息这里分为四种, 主要属性: 网络信息wx.getNetWorkType, 系统信息wx.getSystemInfo, 重力感应数据wx. ...

随机推荐

  1. EditText 空指针问题

    今天在Android中碰到了这样一个问题,其实应该很少人会碰到,因为只有像我这种奇葩才会犯这种错误. 但既然解决了,我就想在这里跟大家分享一下,毕竟它困扰了我一个白天啊...不多说了,看下面... 其 ...

  2. MYSQL 数学运算符问题

    背景: 在mysql中   ’stringA' + 'stringB' 这种类型的操作,在mysql内部会自动转化为两个double 数进行运算. -------------------------- ...

  3. 重启VirtualBox里面的系统提示VT-x features locked or unavailable in MSR错误

    有次不小心设置了一下virtualbox里面的一些配置,然后启动系统时出现了如下提示 在网上找了一些资料尝试了一些方法偶然有一次成功 原来是自己把那个cpu个数设置成了2,改成1就好了,不知道为什么做 ...

  4. 联想企业网盘:SaaS服务集群化持续交付实践

    1      前言 当代信息技术飞速发展,软件和系统的代码规模都变得越来越大,而且组件众多,依赖繁复,每次新版本的发布都仿佛是乘坐一次无座的绿皮车长途夜行,疲惫不堪.软件交付是一个复杂的工程,涉及到软 ...

  5. C++ dynamic_cast实现原理

    dynamic_cast是一个操作符,其用法不再赘述.查看汇编码可以发现实际调用的是这个函数__RTDynamicCast,其内部实现如下: rtti.h: #pragma once extern & ...

  6. C++关键字之static

    一.面向过程设计中的static 1.静态全局变量 在全局变量前,加上关键字static,该变量就被定义成为一个静态全局变量.我们先举一个静态全局变量的例子,如下: [cpp]   #include& ...

  7. MGTemplateEngine 模版引擎简单使用(转)

    原文:http://blog.csdn.net/crazy_srufboy/article/details/21748995 要实现的效果 首先上图中间的 标题至内容 都是使用UIWebView显示, ...

  8. Java入门基础总结(二)

    判断语句 if else  如下: 1 /*    2                    语法: 3              if(条件) 4              { 5          ...

  9. 动画原理——绘画API

    书籍名称:HTML5-Animation-with-JavaScript 书籍源码:https://github.com/lamberta/html5-animation 1.canvas的conte ...

  10. new到底做了什么?

    下面是一个实例化自定义的对象,我们将要对他进行分析 //定义构造函数 function A(){ this.b = 1 //在这个对象里增加一个属性 //不可以拥有返回对象的return语句 } va ...