原文:重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息

[源码下载]

重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息

作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 信息

  • 获取包信息
  • 获取系统信息
  • 获取硬件信息
  • 获取即插即用(PnP: Plug and Play)的设备的信息
  • 获取常用设备信息

示例
1、演示如何获取 app 的 package 信息
Information/PackageInfo.xaml.cs

/*
* 演示如何获取 app 的 package 信息
*/ using System;
using Windows.ApplicationModel;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace XamlDemo.Information
{
public sealed partial class PackageInfo : Page
{
public PackageInfo()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
Package package = Package.Current;
PackageId packageId = package.Id; lblMsg.Text = "Name: " + packageId.Name; // 包名
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Version: " + packageId.Version; // 版本信息
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Architecture: " + packageId.Architecture; // 支持的 cpu 类型(X86, Arm, X64, Neutral(均支持), Unknown)
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Publisher: " + packageId.Publisher; // 发布者
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "PublisherId: " + packageId.PublisherId; // 发布者 id
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "FullName: " + packageId.FullName; // 包全名(Name + Version + Architecture + PublisherId)
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "FamilyName: " + packageId.FamilyName; // 包系列名(Name + PublisherId)
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Installed Location Path: " + package.InstalledLocation.Path; // 包的安装路径
}
}
}

2、演示如何获取系统的相关信息
Information/SystemInfo.xaml.cs

/*
* 演示如何获取系统的相关信息
*/ using System;
using System.Globalization;
using System.Threading.Tasks;
using Windows.Devices.Enumeration.Pnp;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using System.Linq; namespace XamlDemo.Information
{
public sealed partial class SystemInfo : Page
{
public SystemInfo()
{
this.InitializeComponent();
} protected async override void OnNavigatedTo(NavigationEventArgs e)
{
lblMsg.Text += "CPU 核心数量:" + Environment.ProcessorCount.ToString();
lblMsg.Text += Environment.NewLine; lblMsg.Text += "系统自上次启动以来所经过的毫秒数:" + Environment.TickCount;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "当前语言:" + CultureInfo.CurrentCulture.DisplayName;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "当前时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss") + " 星期" + "日一二三四五六七".Substring((int)DateTime.Now.DayOfWeek, );
lblMsg.Text += Environment.NewLine; lblMsg.Text += "当前时区:" + "UTC" + DateTimeOffset.Now.ToString("%K");
lblMsg.Text += Environment.NewLine; lblMsg.Text += "当前系统版本号:" + (await GetWindowsVersionAsync()).ToString();
} #region 获取当前系统版本号,摘自:http://attackpattern.com/2013/03/device-information-in-windows-8-store-apps/
public static async Task<string> GetWindowsVersionAsync()
{
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().ToArray());
}
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;
}
const string DeviceClassKey = "{A45C254E-DF1C-4EFD-8020-67D146A850E0},10";
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";
#endregion
}
}

3、演示如何获取硬件相关的信息
Information/HardwareInfo.xaml.cs

/*
* 演示如何获取硬件相关的信息
*/ using System;
using Windows.Security.ExchangeActiveSyncProvisioning;
using Windows.Storage.Streams;
using Windows.System.Profile;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace XamlDemo.Information
{
public sealed partial class HardwareInfo : Page
{
public HardwareInfo()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
HardwareToken hardwareToken = HardwareIdentification.GetPackageSpecificToken(null); lblMsg.Text = "Id: " + Buffer2Base64(hardwareToken.Id); // 硬件 ID
lblMsg.Text += Environment.NewLine;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Signature: " + Buffer2Base64(hardwareToken.Signature); // 硬件签名
lblMsg.Text += Environment.NewLine;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Certificate: " + Buffer2Base64(hardwareToken.Certificate); // 硬件证书
lblMsg.Text += Environment.NewLine;
lblMsg.Text += Environment.NewLine; EasClientDeviceInformation easClientDeviceInformation = new EasClientDeviceInformation(); lblMsg.Text += "Id: " + easClientDeviceInformation.Id; // 设备 ID
lblMsg.Text += Environment.NewLine;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "FriendlyName: " + easClientDeviceInformation.FriendlyName; // 计算机名
lblMsg.Text += Environment.NewLine;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "OperatingSystem: " + easClientDeviceInformation.OperatingSystem; // 操作系统
lblMsg.Text += Environment.NewLine;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "SystemManufacturer: " + easClientDeviceInformation.SystemManufacturer; // 设备的制造商
lblMsg.Text += Environment.NewLine;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "SystemProductName: " + easClientDeviceInformation.SystemProductName; // 设备的产品名
} private string Buffer2Base64(IBuffer buffer)
{
using (var dataReader = DataReader.FromBuffer(buffer))
{
try
{
var bytes = new byte[buffer.Length];
dataReader.ReadBytes(bytes); return Convert.ToBase64String(bytes);
}
catch (Exception ex)
{
return ex.ToString();
}
}
}
}
}

4、演示如何获取即插即用(PnP: Plug and Play)的设备的相关信息
Information/PnpObjectInfo.xaml

<Page
x:Class="XamlDemo.Information.PnpObjectInfo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Information"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<Grid Margin="120 0 0 0"> <ListBox x:Name="listBox" Margin="0 0 10 10">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock FontWeight="Bold" Text="{Binding Name}" />
<TextBlock Text="{Binding Id}" />
<TextBlock Text="{Binding Properties}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox> </Grid>
</Grid>
</Page>

Information/PnpObjectInfo.xaml.cs

/*
* 演示如何获取即插即用(PnP: Plug and Play)的设备的相关信息
*/ using System;
using Windows.Devices.Enumeration.Pnp;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace XamlDemo.Information
{
public sealed partial class PnpObjectInfo : Page
{
public PnpObjectInfo()
{
this.InitializeComponent();
} protected async override void OnNavigatedTo(NavigationEventArgs e)
{
listBox.Items.Clear(); string[] properties = { "System.ItemNameDisplay", "System.Devices.Manufacturer", "System.Devices.ModelName", "System.Devices.Connected" };
// 通过 PnpObject.FindAllAsync() 获取所有 PnP 设备的指定的信息(更多的属性名称请参见:http://technet.microsoft.com/zh-cn/library/hh464997.aspx)
var containers = await PnpObject.FindAllAsync(PnpObjectType.DeviceContainer, properties); // 显示获取到的 PnP 设备的相关信息
foreach (PnpObject container in containers)
{
listBox.Items.Add(new DisplayItem(container));
}
} /// <summary>
/// 用于保存 PnP 设备的相关信息
/// </summary>
class DisplayItem
{
public string Id { get; private set; }
public string Name { get; private set; }
public string Properties { get; private set; } public DisplayItem(PnpObject container)
{
// 该 PnpObject 的名称
Name = (string)container.Properties["System.ItemNameDisplay"];
if (string.IsNullOrWhiteSpace(Name))
Name = "未知"; // 该 PnpObject 的标识
Id = "Id: " + container.Id; // 该 PnpObject 的信息
foreach (var property in container.Properties)
{
Properties += property.Key + " = " + property.Value + "\n";
}
}
}
}
}

5、演示如何获取常用设备的相关信息
Information/DeviceInfo.xaml

<Page
x:Class="XamlDemo.Information.DeviceInfo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Information"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<Grid Margin="120 0 0 0"> <ListBox x:Name="listBox" Margin="0 0 10 10">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0 0 0 20">
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=Id}" />
<TextBlock Text="{Binding Path=IsEnabled}" />
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="设备缩略图:" />
<Image Width="256" Height="256" Source="{Binding Path=Thumbnail}" Margin="10 0 0 0" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="设备图标:" />
<StackPanel Background="Blue" Margin="10 0 0 0">
<Image Width="48" Height="48" Source="{Binding Path=GlyphThumbnail}" />
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox> </Grid>
</Grid>
</Page>

Information/DeviceInfo.xaml.cs

/*
* 演示如何获取常用设备的相关信息
*/ using System;
using Windows.Devices.Enumeration;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation; namespace XamlDemo.Information
{
public sealed partial class DeviceInfo : Page
{
public DeviceInfo()
{
this.InitializeComponent();
} protected async override void OnNavigatedTo(NavigationEventArgs e)
{
listBox.Items.Clear(); // {0ECEF634-6EF0-472A-8085-5AD023ECBCCD} - 打印设备
// {E5323777-F976-4F5B-9B55-B94699C46E44} - 摄像设备
// {6AC27878-A6FA-4155-BA85-F98F491D4F33} - 便携设备 // 通过 DeviceInformation.FindAllAsync() - 获取指定类型的设备信息
var interfaces = await DeviceInformation.FindAllAsync("System.Devices.InterfaceClassGuid:=\"{6AC27878-A6FA-4155-BA85-F98F491D4F33}\"", null); // 获取全部便携设备的设备信息
// var interfaces = await DeviceInformation.FindAllAsync(DeviceClass.AudioRender); // 通过 DeviceClass 枚举获取指定类型的设备信息 // 显示获取到的常用设备的相关信息
foreach (DeviceInformation deviceInterface in interfaces)
{
DeviceThumbnail thumbnail = await deviceInterface.GetThumbnailAsync();
DeviceThumbnail glyph = await deviceInterface.GetGlyphThumbnailAsync(); listBox.Items.Add(new DisplayItem(deviceInterface, thumbnail, glyph));
} // 创建一个 DeviceWatcher 对象以便在设备发生变化时收到通知
DeviceWatcher deviceWatcher = DeviceInformation.CreateWatcher();
// DeviceWatcher 的相关事件有:Added, EnumerationCompleted, Removed, Stopped, Updated
// DeviceWatcher 的相关方法有:Start(), Stop()
// DeviceWatcher 的相属性件有:Status(一个 DeviceWatcherStatus 类型的枚举)
} /// <summary>
/// 用于保存常用设备的相关信息
/// </summary>
class DisplayItem
{
public string Name { get; private set; }
public string Id { get; private set; }
public string IsEnabled { get; private set; }
public BitmapImage Thumbnail { get; private set; }
public BitmapImage GlyphThumbnail { get; private set; } public DisplayItem(DeviceInformation deviceInterface, DeviceThumbnail thumbnail, DeviceThumbnail glyph)
{
// 设备名称
Name = (string)deviceInterface.Properties["System.ItemNameDisplay"]; // 设备标识
Id = "ID: " + deviceInterface.Id; // 设备是否已启用
IsEnabled = "IsEnabled: " + deviceInterface.IsEnabled; // 设备缩略图
Thumbnail = new BitmapImage();
Thumbnail.SetSource(thumbnail); // 设备图标
GlyphThumbnail = new BitmapImage();
GlyphThumbnail.SetSource(glyph);
}
}
}
}

OK
[源码下载]

重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息的更多相关文章

  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 Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  4. 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo

    [源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...

  5. 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解

    [源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...

  6. 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract

    [源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  7. 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract

    [源码下载] 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 ...

  8. 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract

    [源码下载] 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之  ...

  9. 重新想象 Windows 8 Store Apps (41) - 打印

    [源码下载] 重新想象 Windows 8 Store Apps (41) - 打印 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 打印 示例1.需要打印的文档Pr ...

随机推荐

  1. Pyhon安装media模块

    都是教科书惹的祸,它没有说清楚.media看着很标准,其实不是python自带的库.需要安装第三方软件后才能用. 在这里http://pythonhosted.org/PyGraphics/insta ...

  2. PHP网站安装程序的原理及代码

    原文:PHP网站安装程序的原理及代码 原理: 其实PHP程序的安装原理无非就是将数据库结构和内容导入到相应的数据库中,从这个过程中重新配置连接数据库的参数和文件,为了保证不被别人恶意使用安装文件,当安 ...

  3. python安装依赖

    yum install zlib zlib-devel openssl openssl-devel bzip2 bzip2-devel ncurses ncurses-devel readline r ...

  4. 跨平台网络通信与server编程框架库(acl库)介绍

    一.描写叙述 acl project是一个跨平台(支持LINUX,WIN32,Solaris,MacOS,FreeBSD)的网络通信库及server编程框架,同一时候提供很多其它的有用功能库.通过该库 ...

  5. (hdu 简单题 128道)平方和与立方和(求一个区间的立方和和平方和)

    题目: 平方和与立方和 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. TopCoder SRM 625 Incrementing Sequence 题解

    本题就是给出一个数k和一个数组,包含N个元素,通过每次添加�数组中的一个数的操作,最后须要得到1 - N的一个序列,不用排序. 能够从暴力法入手,然后优化. 这里利用hash表进行优化,终于得到时间效 ...

  7. hdu1881(贪心+dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1881 分析:按照结束时间从小到大排序,然后以每个结束点为容量进行01背包,选入的必定符合条件的. 因为 ...

  8. Hdu 4738【求无向图的桥】.cpp

    题目: 曹操在长江上建立了一些点,点之间有一些边连着.如果这些点构成的无向图变成了连通图,那么曹操就无敌了.刘备为了防止曹操变得无敌,就打算去摧毁连接曹操的点的桥.但是诸葛亮把所有炸弹都带走了,只留下 ...

  9. OpenCL 查看设备信息

    好久没搞OpenCL了.可是这是个好东西.不能不学,之前发了篇设置OpenCL的文章.看的人还真多,看来大家都知道这个好东西了,都想把OpenCL搞起.只是学习难度还是相当高的. 之前忙搞算法,所以非 ...

  10. ubuntu14.04中 gedit 凝视能显示中文,而source insight中显示为乱码的解决的方法

    1.乱码显示情况: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcjc3NjgzOTYy/font/5a6L5L2T/fontsize/400/fill/ ...