Get the Hardware ID of a Device in Windows Runtime Apps

Step 1

First create a blank Windows Project. Name it ‘UniqueHardwareId’.

Step 2

In MainPage.xaml add a button with click event and a textbox to display the hardware id:

Complete XAML code

  1. <Page  
  2. x:Class="UniqueHardwareId.MainPage"  
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5. xmlns:local="using:UniqueHardwareId"  
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8. mc:Ignorable="d">  
  9.   
  10. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  11. <Button x:Name="btnGetHwId" Content="Get Hardware ID" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="353,250,0,0" Height="99" FontSize="35" Width="743" Click="btnGetHwId_Click"/>  
  12. <TextBox x:Name="txtId" HorizontalAlignment="Left" FontSize="30" TextWrapping="Wrap" VerticalAlignment="Top" Margin="356,409,0,0" Height="87" Width="737"/>  
  13.   
  14. </Grid>  
  15. </Page>  
Step 3

In the code behind MainPage.xaml.cs, update code with, 
  1. using System;  
  2. using Windows.Storage.Streams;  
  3. using Windows.System.Profile;  
  4. using Windows.UI.Xaml;  
  5. using Windows.UI.Xaml.Controls;  
  6. namespace UniqueHardwareId   
  7. {  
  8.     public sealed partial class MainPage: Page   
  9.     {  
  10.         public MainPage()   
  11.         {  
  12.             this.InitializeComponent();  
  13.         }  
  14.         private void btnGetHwId_Click(object sender, RoutedEventArgs e)   
  15.         {  
  16.             try   
  17.             {  
  18.                 var hardwareToken = HardwareIdentification.GetPackageSpecificToken(null);  
  19.                 var hardwareId = hardwareToken.Id;  
  20.                 var dataReader = DataReader.FromBuffer(hardwareId);  
  21.                 byte[] bytes = new byte[hardwareId.Length];  
  22.                 dataReader.ReadBytes(bytes);  
  23.                 txtId.Text = BitConverter.ToString(bytes);  
  24.             } catch (Exception ex) {}  
  25.         }  
  26.     }  
  27. }  
Step 4

Run the application, click on the button. You see the Hardware Id in the textbox field.

application

You can also get the complete project from GitHub.
Next Recommended Reading Windows 8 Grid View control metro UI app