Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a unique identifier for a machine running Windows 8 in C#?

I'm working on a Metro app written in C# and need a way to uniquely identify a device. I found the ASHWID in the documentation which looks great. The code suggested is as follows:

HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null);
IBuffer hardwareId = token.Id;
IBuffer signature = token.Signature;
IBuffer certificate = token.Certificate;

The problem is, how do I turn that IBuffer into a string which I can use?

like image 398
StarlitSkies Avatar asked Sep 21 '12 09:09

StarlitSkies


1 Answers

After a lot of hunting through suggestions which were actually in JS or C++ I finally found an answer!

private string GetHardwareId()  
{  
    var token = HardwareIdentification.GetPackageSpecificToken(null);  
    var hardwareId = token.Id;  
    var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);  

    byte[] bytes = new byte[hardwareId.Length];  
    dataReader.ReadBytes(bytes);  

    return BitConverter.ToString(bytes);  
}  

Thanks go to this blog - http://bartwullems.blogspot.co.uk/2012/09/windows-8-uniquely-identifying-device.html

like image 166
StarlitSkies Avatar answered Sep 28 '22 06:09

StarlitSkies