How can I get Operating system details like OS Serial Number
(OS Product key), User Domain Name
,User Name
and PC Full Name
?
What is the best way and optimal way to get it?

- 4,006
- 7
- 30
- 47
-
What is the version of Windows you are using? – bonCodigo Dec 13 '12 at 06:39
-
Possible duplicate: http://stackoverflow.com/questions/4742389/get-pc-system-information-on-windows-machine-c-sharp-script – Dennis Dec 13 '12 at 06:39
-
Which "serial number" are you trying to get? Windows, motherboard, etc? – ceyko Dec 13 '12 at 06:48
-
Is it just a coincidence that your name is an anagram of a codeplex project for extracting a product key from windows - http://wpkf.codeplex.com/ ? Regardless, as I've mentioned in my updated answer, it may be able to help you, especially being .NET and open source. – ceyko Dec 13 '12 at 07:24
-
It is Just a coincidence :) – Kishor Dec 13 '12 at 07:44
3 Answers
System.Environment
Check out the (static) System.Environment
class.
It has properties for MachineName
, UserDomainName
, and UserName
.
System.Management
If you're looking for the BIOS serial number (or lots of info on other hardware) you might try the System.Management
namespace, specifically 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 info about the machine by querying, for example, on Win32_Processor
or others as listed on MSDN. This is using WMI
via WQL
.
Windows product key via registry
For an 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 decoded to get 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);
}
Alternatively, I found an open source c# project that supposedly can extract the product key for any version of windows: http://wpkf.codeplex.com/ It uses the method given above and provides some additional information about the machine.

- 4,822
- 1
- 18
- 23
You need to use IPGlobalProperties.GetIPGlobalProperties Method To get network related information:
var conInfo = IPGlobalProperties.GetIPGlobalProperties();
Console.WriteLine(conInfo.HostName);
Console.WriteLine(conInfo.DomainName);
...
For Machine Name use Environment.MachineName Property:
Console.WriteLine(System.Environment.MachineName);

- 3,795
- 2
- 19
- 22
-
Nope, this method cant even get the IP address, and the OP is asking for the Windows Key. – Jhollman Jul 11 '20 at 13:37
Did you try the Systeminformation class it provides alot of information about the current system environment, take a look at the example on MSDN site. It has the followign properties:
ComputerName, UserDomianName, UserName ...etc

- 36,908
- 70
- 97
- 130
-
-
SystemInformation Class, only useful information is those 3 you said (ComputerName, UserDomianName, UserName) and Screen Resolution, NOTHING ELSE, Windows Product key from this? Not even close to it. – Jhollman Jul 11 '20 at 13:36