0

I am making a concept of registry viewer that gets all the names
of the subkey in the registry and lists all the names in the console. and on the
string[] subKeyNames = jdfddnjdz.GetSubKeyNames();
i get an error which is Object reference not set to an instance of an object.

here's the code.

RegistryKey jdfddnjdz = Registry.CurrentUser.OpenSubKey(@"\SOFTWARE\Example\");
string[] subKeyNames = jdfddnjdz.GetSubKeyNames();
foreach (String val5 in subKeyNames)
{
    Console.WriteLine(val5);
    Console.ReadKey();
}

Edit:
This code works fine but if I add .OpenSubKey like in the code above this it gives an error.

RegistryKey subKey = Registry.CurrentUser;
string[] subkeyNemez = subKey.GetSubKeyNames();
foreach (String valuef in subkeyNemez)
{
    Console.WriteLine(valuef);
    Console.ReadKey();
}
YakovL
  • 5,213
  • 10
  • 46
  • 71
CursedSheep
  • 109
  • 1
  • 1
  • 10
  • That horribly named variable is null. Project > Properties > Build tab, untick the "Prefer 32-bit" checkbox. And add the null test. – Hans Passant Mar 18 '18 at 13:03
  • 1
    `OpenSubKey` returns `null` when either the key does not exist or the application does not have access (not running as admin) – Camilo Terevinto Mar 18 '18 at 13:03
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Camilo Terevinto Mar 18 '18 at 13:03

2 Answers2

0
if (jdfddnjdz != null){
... your code
}
Jamez Fatout
  • 302
  • 3
  • 12
0

I finally solved it after a week! Just need to remove slash/Backslash on the "Software".

            RegistryKey jdfddnjdz = Registry.CurrentUser;
        jdfddnjdz = jdfddnjdz.OpenSubKey(@"Software\Microsoft");
        string[] dshg = jdfddnjdz.GetSubKeyNames();
        foreach(String umm in dshg)
        {
            Console.WriteLine(umm);
            break;
        }
        Console.ReadKey();
CursedSheep
  • 109
  • 1
  • 1
  • 10