c# - 从注册表读取 DigitalProductId 返回 null

标签 c# null registry

我目前正在尝试从注册表获取 Windows 7 安装的产品 key 。但由于某种原因 DigitalProductId 返回 null。我不明白为什么要这样做。

我可以很好地读取“NT\Current Version”中的其他值。就像“productName”,但当涉及到 digitalProductId 时,我什么也没得到。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Collections;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            byte[] results = Form1.GetRegistryDigitalProductId(Form1.Key.Windows);
            string cdKey = Form1.DecodeProductKey(results);
            MessageBox.Show(cdKey, "HWID()", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


        public enum Key { Windows };
        public static byte[] GetRegistryDigitalProductId(Key key)
        {
            byte[] digitalProductId = null;
            RegistryKey registry = null;
            switch (key)
            {
                case Key.Windows:
                    registry =
                      Registry.LocalMachine.
                        OpenSubKey(
                          @"SOFTWARE\Microsoft\Windows NT\CurrentVersion",
                            false);
                    break;
            }
            if (registry != null)
            {
                digitalProductId = registry.GetValue("DigitalProductId")
                  as byte[];
                registry.Close();
            }
            return digitalProductId;
        }
        public static string DecodeProductKey(byte[] digitalProductId)
        {
            const int keyStartIndex = 52;
            const int keyEndIndex = keyStartIndex + 15;
            char[] digits = new char[]
     {
       'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'P', 'Q', 'R', 
       'T', 'V', 'W', 'X', 'Y', '2', '3', '4', '6', '7', '8', '9',
     };
            const int decodeLength = 29;
            const int decodeStringLength = 15;
            char[] decodedChars = new char[decodeLength];
            ArrayList hexPid = new ArrayList();
            for (int i = keyStartIndex; i <= keyEndIndex; i++)
            {
                hexPid.Add(digitalProductId[i]);
            }
            for (int i = decodeLength - 1; i >= 0; i--)
            {
                if ((i + 1) % 6 == 0)
                {
                    decodedChars[i] = '-';
                }
                else
                {
                    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# 新手,我正在尝试遵循本指南。 http://www.ultimateprogrammingtutorials.info/2013/05/how-to-get-windows-product-key-in-c.html

但我无法正常工作。

谢谢

最佳答案

如果您的构建目标是 64 位,则可以按原样使用代码。


否则,您会被引导至此处,并且可能找不到您要查找的 key :

SOFTWARE\Wow6432Node\...

因此,如果您必须以 32 位为目标,请使用此代码:

var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);

var reg = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);

var digitalProductId = reg.GetValue("DigitalProductId") as byte[];

关于c# - 从注册表读取 DigitalProductId 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22205506/

相关文章:

batch-file - 一行获取 Reg 查询的错误代码

windows - RegOpenKeyEx 在失败时返回什么错误代码?

c# - 如何使用 asp.net mvc 5 在一个页面上使用多个模型?

c# - asp.net MVC 如何从 POST 接收数据

c# - DLL 的 app.config 中的 ConnectionString 为空

go - 为什么 Go 中的错误可以为零?

TypeScript set string = null 失败,但是 null!作品

c# - 如何加载用户设置和值

javascript - 在脚本末尾发布引用

.net - 应用程序配置与注册表? .NET 中的首选方法是什么?