获取Windows详细信息,如产品密钥、域名、用户名等。

4

我该如何获取操作系统的详细信息,比如操作系统的序列号(产品密钥),用户域名,用户名和PC全名?最佳和最优方法是什么?


你正在使用哪个版本的Windows? - bonCodigo
可能是重复问题:https://dev59.com/mm445IYBdhLWcg3wq8Pp - Dennis
你想获取哪个“序列号”?Windows、主板还是其他的? - ceyko
你的名字恰好是从Windows中提取产品密钥的CodePlex项目wpkf.codeplex.com的一个变形词,这只是巧合吗?无论如何,正如我在更新的答案中提到的那样,它可能会对你有所帮助,特别是因为它是.NET和开源的。 - ceyko
这只是巧合 :) - Kishor
3个回答

9

System.Environment

请查看(静态)System.Environment类。

它有一些属性如MachineName, UserDomainName, 和 UserName

System.Management

如果您正在寻找BIOS序列号(或其他硬件的大量信息),可以尝试System.Management命名空间,特别是SelectQueryManagementObjectSearcher

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实现的。

通过注册表获得Windows产品密钥

在许多版本的Windows中,操作系统序列号存储在注册表中的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/ 它使用上述方法并提供有关计算机的一些附加信息。


-1

不行,这个方法甚至无法获取IP地址,而且OP正在寻找Windows密钥。 - Jhollman

-1

请注意,这取决于WinForms。 - ceyko
SystemInformation类,有用的信息只有你提到的那三个(ComputerName、UserDomianName和UserName)以及屏幕分辨率,没有其他的。从这里获取Windows产品密钥?根本不可能。 - Jhollman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接