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

最新文章

  1. JDK下载、安装、配置环境变量笔记
  2. Unity 5 WebGL vs Web Player
  3. php计算中英文混搭字符串长度
  4. Windows Store App 控件动画
  5. centos 常用命令
  6. MiniDao-PE精简版
  7. codeforces B. Levko and Permutation 解题报告
  8. XML学习经验实例总结2
  9. node 当中的 cnpm和npm 的区别和使用
  10. 个人从源码理解angular项目在JIT模式下的启动过程
  11. Javascript中的Microtask和Macrotask——从一道很少有人能答对的题目说起
  12. Python中模块之logging &amp; subprocess的讲解
  13. 【VSTS 日志 15/11/18】 – 插件应用市场,RM,包管理器等
  14. (译)WebRTC实战: STUN, TURN, Signaling
  15. ElasticSsarch汇总
  16. 一.MySQL安装
  17. ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction解决办法
  18. IntelliJ IDEA配置maven远程仓库
  19. 实验隐藏参数&quot;_allow_resetlogs_corruption&quot;的使用
  20. git之概念图

热门文章

  1. 在 WinForm 中打开页面采用POST方式传参http。可以多个参数传递,返回json字符串
  2. Uber License for Android
  3. CSS自学笔记(11):CSS3背景和边框
  4. Paas
  5. perl 公交车查询
  6. Linux Makefile文件编写详细步骤与实践
  7. 剖析magento中关于Email模板的设置
  8. Net常用命名空间和类介绍
  9. AngularJS学习资源
  10. win10 ie11 以管理员身份运行才正常