Question registry operation

Juan

Member
Joined
Dec 25, 2018
Messages
8
Programming Experience
Beginner
Hello,
I'm trying to make my first program.
C#:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;


namespace HelloWorld
{
    class Hello
    {
        static void Main()
        {

Console.WriteLine(result);


                       
        }
        private static string GetWindowsProductKey()        {
            string result = string.Empty;
            byte[] array = null;
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", false);
            if (registryKey != null)
            {
                array = (registryKey.GetValue("DigitalProductId") as byte[]);
                registryKey.Close();
            }
            if (array != null)
            {
                result = Hardware.DecodeProductKey(array);
            }
            return result;
        }

How to fix it?
Thank you.
 
Last edited:
I have these errors:

Error 1 The best overloaded method match for 'System.Console.WriteLine(bool)' has some invalid arguments
Error 2 Argument 1: cannot convert from 'method group' to 'bool'
 
The problem is that, on this line:
Console.WriteLine(result);

you are trying to output the value of 'result' but the compiler doesn't know what 'result' is in that context. The only place you have declared something named 'result' is a local variable inside the GetWindowsProductKey but local variables cannot be seen from outside the method they're declared in so it cannot be that. Given that the GetWindowsProductKey method returns that value, the logical thing to do seems to be to output the result of the GetWindowsProductKey. You could do that directly:
Console.WriteLine(GetWindowsProductKey());

or you could assign the return value to a variable and output that:
string result = GetWindowsProductKey();

Console.WriteLine(result);

Note that, in the second case, I'm using a variable named 'result' but it is not the same variable. That variable exists only within the Main method while the other one exists only within the GetWindowsProductKey method.
 
The problem is that, on this line:
Console.WriteLine(result);

you are trying to output the value of 'result' but the compiler doesn't know what 'result' is in that context. The only place you have declared something named 'result' is a local variable inside the GetWindowsProductKey but local variables cannot be seen from outside the method they're declared in so it cannot be that. Given that the GetWindowsProductKey method returns that value, the logical thing to do seems to be to output the result of the GetWindowsProductKey. You could do that directly:
Console.WriteLine(GetWindowsProductKey());

or you could assign the return value to a variable and output that:
string result = GetWindowsProductKey();

Console.WriteLine(result);

Note that, in the second case, I'm using a variable named 'result' but it is not the same variable. That variable exists only within the Main method while the other one exists only within the GetWindowsProductKey method.
Thank you. I rewrite this program.Now I don't any error, but it did not return the value.....
C#:
using System;using System.Collections;
using System.Management;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Win32;
namespace Juan
{
    class Program
    {
        public static void Main(string[] args)
        {


            string resultado = string.Empty;
            resultado = GetWindowsProductKey();


            


            


            Console.WriteLine("This is the ProductKey: " + resultado);
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
        public static string GetWindowsProductKey()
        {
            string result = string.Empty;
            byte[] array = null;
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", false);
            if (registryKey != null)
            {
                array = (registryKey.GetValue("DigitalProductId") as byte[]);
                registryKey.Close();
            }
            if (array != null)
            {
                result = DecodeProductKey(array); // result = Hardware.DecodeProductKey(array);
            }
            return result;
        }








        public static string DecodeProductKey(byte[] digitalProductId)
        {
            char[] array = 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'
};
            char[] array2 = new char[29];
            ArrayList arrayList = new ArrayList();
            for (int i = 52; i <= 67; i++)
            {
                arrayList.Add(digitalProductId[i]);
            }
            for (int i = 28; i >= 0; i--)
            {
                if ((i + 1) % 6 == 0)
                {
                    array2[i] = '-';
                }
                else
                {
                    int num = 0;
                    for (int j = 14; j >= 0; j--)
                    {
                        int num2 = num << 8 | (int)((byte)arrayList[j]);
                        arrayList[j] = (byte)(num2 / 24);
                        num = num2 % 24;
                        array2[i] = array[num];
                    }
                }
            }
            return new string(array2);
        }
    }
}
 
Back
Top Bottom