我该如何获取操作系统的详细信息,比如操作系统的序列号(产品密钥),用户域名,用户名和PC全名?最佳和最优方法是什么?
我该如何获取操作系统的详细信息,比如操作系统的序列号(产品密钥),用户域名,用户名和PC全名?最佳和最优方法是什么?
请查看(静态)System.Environment
类。
它有一些属性如MachineName
, UserDomainName
, 和 UserName
。
如果您正在寻找BIOS序列号(或其他硬件的大量信息),可以尝试System.Management
命名空间,特别是SelectQuery
和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;
}
Win32_Processor
或其他在MSDN上列出的信息,获取有关计算机的其他信息。 这是使用WMI
通过WQL
实现的。
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId
,但它以某种编码形式存在,并且需要解码才能获取产品密钥。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);
}
或者,我发现了一个开源的C#项目,据说可以提取任何版本Windows的产品密钥:http://wpkf.codeplex.com/ 它使用上述方法并提供有关计算机的一些附加信息。
你需要使用 IPGlobalProperties.GetIPGlobalProperties方法 获取与网络相关的信息:
var conInfo = IPGlobalProperties.GetIPGlobalProperties();
Console.WriteLine(conInfo.HostName);
Console.WriteLine(conInfo.DomainName);
...
对于机器名称,请使用Environment.MachineName属性:
Console.WriteLine(System.Environment.MachineName);
ComputerName, UserDomianName, UserName ...等等