Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#获取Windows CD密钥_C#_Windows 7_Registry - Fatal编程技术网

C#获取Windows CD密钥

C#获取Windows CD密钥,c#,windows-7,registry,C#,Windows 7,Registry,我一直在使用下面的代码来获取Windows许可证密钥。它在很长一段时间内运行良好。但现在我发现它可以在WindowsXP(x86)上运行,但不能在Windows7x64上运行 原因:在64位操作系统上查找的范围内,DigitalProductIDRegisity值只包含零。因此,结果是bbbbbbb-BBBBB-BBBBB-BBBBB-BBBBB-bbbbbbb。为什么会这样?我怎样才能解决这个问题 public static string LicenseCDKey {

我一直在使用下面的代码来获取Windows许可证密钥。它在很长一段时间内运行良好。但现在我发现它可以在WindowsXP(x86)上运行,但不能在Windows7x64上运行

原因:在64位操作系统上查找的范围内,
DigitalProductID
Regisity值只包含零。因此,结果是
bbbbbbb-BBBBB-BBBBB-BBBBB-BBBBB-bbbbbbb
。为什么会这样?我怎样才能解决这个问题

    public static string LicenseCDKey
    {
        get
        {
            try
            {
                byte[] rpk = (byte[])Registry.LocalMachine
                   .OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion")
                   .GetValue("DigitalProductId");
                string serial = "";
                const string possible = "BCDFGHJKMPQRTVWXY2346789";
                for (int i = 0; i < 25; i++)
                {
                    int accu = 0;
                    for (int a = 0; a < 15; a++)
                    {
                        accu <<= 8;
                        accu += rpk[66 - a];
                        rpk[66 - a] = (byte)(accu / 24 & 0xff);
                        accu %= 24;
                    }
                    serial = possible[accu] + serial;
                    if (i % 5 == 4 && i < 24)
                    {
                        serial = "-" + serial;
                    }
                }
                return serial;
            }
            catch
            {
                return ErrorString;
            }
        }
    }
公共静态字符串许可证CDKey
{
得到
{
尝试
{
字节[]rpk=(字节[])Registry.LocalMachine
.OpenSubKey(@“软件\Microsoft\Windows NT\CurrentVersion”)
.GetValue(“DigitalProductId”);
字符串serial=“”;
常量字符串可能=“BCDFGHJKMPQRTVWXY2346789”;
对于(int i=0;i<25;i++)
{
int accu=0;
对于(int a=0;a<15;a++)
{

accu32位应用程序使用不同的注册表路径

32位应用程序访问中的注册表路径 HKEY\U LOCAL\U MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion 找不到产品密钥


将处理器类型更改为x64对我来说很有用,因为user287107指出在x64操作系统上运行的x86应用程序(32位)使用的是不同的注册表(注册表视图)

要访问x64注册表,您有几个选项:

  • 将平台目标更改为x64(Visual Studio项目设置)
  • 如果您使用的是.Net Framework 4.0,则可以使用
    RegistryKey
    类和
    RegistryView
    enum访问x64注册表

    RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                                              RegistryView.Registry64);
    
    string keyPath = @"Software\Microsoft\Windows NT\CurrentVersion";
    byte[] rpk = (byte[])key.OpenSubKey(keyPath).GetValue("DigitalProductId");
    
  • 如果您不使用.Net Framework 4.0,并且不希望将平台目标设置为x64,则必须使用Interop(
    RegOpenKeyEx()
    Win32 API函数和
    KEY\u WOW64\u 32KEY
    标志)访问x64注册表

  • RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                                              RegistryView.Registry64);
    
    string keyPath = @"Software\Microsoft\Windows NT\CurrentVersion";
    byte[] rpk = (byte[])key.OpenSubKey(keyPath).GetValue("DigitalProductId");
    
    开始编辑

    我刚刚发现了一个有趣的解释,解释了为什么DigitalProductId键可以为null/空:

  • 您正在使用批量许可证密钥激活Windows 7操作系统。激活后,VLC密钥将从注册表中删除
  • 有人使用命令
    slmgr–cpky

  • 结束编辑

    您是否将平台目标的输出更改为x64而不是任何CPU?没有,正如我对下面两个答案的回答所述,我使用的是AnyCPU。我确实使用AnyCPU,但仍然不费吹灰之力。对于x86,我根本无法获得任何密钥。而且,正如user287107的回答中所述,我确实使用AnyCPU,但它仍然不起作用。请尝试代码yoursef and see.@DevilsChild:客户端计算机是使用批量许可证密钥激活的吗?@DevilsChild:在我的x64位Windows 7计算机上,上面的代码返回正确的许可证密钥。@DevilsChild:如果您不使用批量许可证密钥,则我认为DigitalProductId已使用slmgr-cpky命令删除。注意,注册表项仍然存在但是修改了DigitalProductId。因此您得到了BBBBB-BBBBB-BBBBB-BBBBB-bbbbbbb.Ok,因此这不是我的代码中的错误,而是用户机器上的错误。谢谢您的帮助。