Reputation: 4096
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?
Upvotes: 4
Views: 8583
Reputation: 4852
Check out the (static) System.Environment
class.
It has properties for MachineName
, UserDomainName
, and UserName
.
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
.
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.
Upvotes: 9
Reputation: 3695
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);
Upvotes: -1
Reputation: 37576
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
Upvotes: -1