1

I am trying to read another applications registry value but I am not having much success. I have searched a full page and a half of results on Google, but not found what I am looking for.

I am trying to read the following registry value:

\\HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\AVG CloudCare\DisplayVersion

A typical value would be: '3.5.3'

I am using this code which I found online:

RegistryKey RegInfo = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\AVG CloudCare", false);

object CCVersionReg = RegInfo.GetValue("DisplayVersion", "0");

Console.WriteLine(CCVersionReg);
Console.ReadLine();

However I always get an error: "Object reference not set to an instance of an object.".

I tried to convert the object to a string like this:

Console.WriteLine(CCVersionReg.ToString);

This also doesn't work, with the error, "Cannot convert to 'method group' from 'bool'.

The reason I need this is to compare this version with the known latest version for an updater application, which I need to use string to compare the values.

user2924019
  • 1,983
  • 4
  • 29
  • 49

3 Answers3

2

Try this

var key = @"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\AVG CloudCare";
Console.WriteLine(Registry.GetValue(key, "DisplayVersion", "0")?.ToString());

When ToString() is invoked on a null object, it throws Object reference not set to an instance of an object

Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
2

Your error

Console.WriteLine(CCVersionReg.ToString);

is throwing that error because CCVersion.ToString is a method group. You will need to invoke the method by doing this:

Console.WriteLine(CCVersionReg.ToString());

However, you will get a null reference exception(as you are seeing) if CCVersionReg is null. Check that it gets created properly

Dudemanword
  • 710
  • 1
  • 6
  • 20
0

Is your application 32-bit running on a 64-bit OS ? If so, please see: OpenSubKey() returns null for a registry key that I can see in regedit.exe

Astx
  • 11
  • 1
  • 3