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

StarlitSkies picture StarlitSkies · Sep 21, 2012 · Viewed 7.2k times · Source

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?

Answer

StarlitSkies picture StarlitSkies · Sep 21, 2012

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