我正在试图让我的我的Windows产品密钥从注册表7安装。但由于某种原因DigitalProductId返回null。而我无法弄清楚为什么这样做。阅读DigitalProductId注册表从回来空
我可以在“NT \当前版本”蛮好看其他值。比如“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位。但是,当我去“SOFTWARE \ Wow6432Node \微软\的Windows NT \ CURRENTVERSION”注册表编辑器中没有列出的......(也许我没有完全理解你说的话)另外,DigitalProductId如果你重写开关(关键)正常工作,看起来像什么?谢谢回复。 – Throdne
没关系。在玩完你的代码之后,我能够理解你在说什么。我得到它正常工作。谢谢您的帮助。现在我明白你必须为每个构建做些什么。 – Throdne