【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平板。
【Windows 8 Store App】学习一:获取设备信息的更多相关文章
- 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop
[源码下载] 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 作者:weba ...
- 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化
[源码下载] 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...
- 【Windows 8 Store App】学习:目录
原文http://www.cnblogs.com/java-koma/archive/2013/05/22/3093302.html 写在前面:我之前从事java开发,对MS的一整套东西还没入门哈,难 ...
- 小白学phoneGap《构建跨平台APP:phoneGap移动应用实战》连载五(使用PhoneGap获取设备信息)
除了能够将HTML页面打包成可以直接安装运行的APP外,PhoneGap的一个最大优势在于可以通过JavaScript调用设备来访问设备上的硬件信息,从而实现一些原本只有依靠原生SDK才能够达到的目的 ...
- React Native(五)——获取设备信息react-native-device-info
心酸史: 自从接触rn开始后,越来越多的引入第三方组件而开始的配置文件,让自己一再头疼: 明明是按照官方文档一步一步的配置,为什么别人可以做到的自己却屡屡出错,真是哭笑不得--从微信分享react-n ...
- appium自动化测试框架——封装获取设备信息类
在上一节中,我们已经解决了如何在python中执行cmd,并获取执行结果.下面就小小实战一下,获取设备信息. 一.思路 1.windows上获取设备信息的方法 输入dos命令“adb devices” ...
- Android 获取设备信息 异常
/**获取设备信息 * @param c * @return */ public static void setDeviceInfo(Context c,RequestParams params){ ...
- 微信小程序 --- 获取设备信息
获取设备信息: wx.getSystemInfo model:手机型号 pixelRatio:设备像素比 windowWidth:窗口宽度 windowHeight:窗口高度 language:语言 ...
- PhoneGap获取设备信息
一. 获取设备信息的方法列表(如果没有或者检测不出来就显示undefined) 1.device.name 设备名称(一些国产机检测不出来) 2.device.model ...
- 微信小程序把玩(三十八)获取设备信息 API
原文:微信小程序把玩(三十八)获取设备信息 API 获取设备信息这里分为四种, 主要属性: 网络信息wx.getNetWorkType, 系统信息wx.getSystemInfo, 重力感应数据wx. ...
随机推荐
- Excel转JSON-简单-暴力-迅速
一直在做一个关于网上选课的系统,选用了时下比较流行的node.js.今天在想怎么把学生或者老师的信息导入进去,涉及数量比较多一点,我手边又正好有一部分excel的表格.就想把excel转成json然后 ...
- EditText默认不弹出软键盘
#EditText默认不弹出软键盘# 网上关于EditText默认情况下不弹出软键盘,当手触摸到EditText,获取焦点时候,才会弹出软键盘,貌似都不能用,其实,在oncreate()方法中,加上 ...
- shell基础——变量定义
快速参考: 变量定义格式: 变量名=值 str1="hello world" # define a string var str2=hello # define a string ...
- Ubuntu之修改用户名和主机名
记得曾几何时,想把自己电脑的“乌班兔儿”取个响亮的名字,但是问了很久度娘和谷哥,都要我把当前用户删除了(userdel -r xxx),重新建一个用户(adduser xxx),但是,我的电脑是所有环 ...
- Android UI ActionBar功能-Action Bar 左上角的向上或返回按钮
ActionBar在左上角还提供了一个向上或返回的按钮,默认情况下是隐藏的需要在代码中开启: 官方文档:http://wear.techbrood.com/training/basics/action ...
- skiing(搜索+记忆化搜索)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- find the safest road
find the safest road Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Othe ...
- vs2010:【“System.Data.OracleClient.OracleConnection”已过时】警告
在oracle 安装目录下 找到 Oracle.DataAccess.dll添加引用,然后 using Oracle.DataAccess.Client;其他的都不用动,即可.连接字符串中 如有 用的 ...
- TFS跨版本Merge测试
原始文件Merge.txt Change Set Dev Beta #1 2014-9-25 10:49:13 ZDK 2014-9-25 10:49:13 ZDK #2 2014-9-25 10:4 ...
- How to close existing connections to a DB
use master ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE --do you stuff here A ...