Windows 8 Store Apps學習(30) 資訊

來源:互聯網
上載者:User

資訊: 擷取包資訊, 系統資訊, 硬體資訊, PnP資訊, 常用裝置資訊

介紹

重新想象 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, 1);            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(2).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.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); // 硬體識別碼            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); // 硬體認證        }            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();                }            }        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.