The statement
if (key == null)
in your code, means that you will only execute the following code if your call to
OpenSubKey
fails. So you are guaranteed to get that error. It should be
if (key != null)
And you should also investigate why your call to
OpenSubKey
is failing.