System.Environment
Check out the (static) System.Environment .
It has properties for MachineName , UserDomainName and UserName .
System.management
If you are looking for the BIOS serial number (or a lot of information on other hardware), you can try the System.Management namespace, in particular SelectQuery and ManagementObjectSearcher .
var query = new SelectQuery("select * from Win32_Bios"); var search = new ManagementObjectSearcher(query); foreach (ManagementBaseObject item in search.Get()) { string serial = item["SerialNumber"] as string; if (serial != null) return serial; }
You can get other information about the machine by Win32_Processor , for example, on Win32_Processor or others, as indicated in MSDN . This is used by WMI through WQL .
Windows product key through registry
For the OS serial number in many versions of Windows, it is stored in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId , however it is in some encoded form and needs to be decrypted to obtain the product key.
You can use the following method to decode this value found here (but slightly modified for clarity).
public string DecodeProductKey(byte[] digitalProductId) { // Possible alpha-numeric characters in product key. const string digits = "BCDFGHJKMPQRTVWXY2346789"; // Length of decoded product key in byte-form. Each byte represents 2 chars. const int decodeStringLength = 15; // Decoded product key is of length 29 char[] decodedChars = new char[29]; // Extract encoded product key from bytes [52,67] List<byte> hexPid = new List<byte>(); for (int i = 52; i <= 67; i++) { hexPid.Add(digitalProductId[i]); } // Decode characters for (int i = decodedChars.Length - 1; i >= 0; i--) { // Every sixth char is a separator. if ((i + 1) % 6 == 0) { decodedChars[i] = '-'; } else { // Do the actual decoding. int digitMapIndex = 0; for (int j = decodeStringLength - 1; j >= 0; j--) { int byteValue = (digitMapIndex << 8) | (byte)hexPid[j]; hexPid[j] = (byte)(byteValue / 24); digitMapIndex = byteValue % 24; decodedChars[i] = digits[digitMapIndex]; } } } return new string(decodedChars); }
As an alternative, I found an open source C # project that supposedly can extract a product key for any version of windows: http://wpkf.codeplex.com/ It uses the above method and provides some additional information about the machine.
source share