Accessing a Registry Key is Failing from .Net Application

954 views Asked by At

I am trying to access a registry key in LocalMachine. The code works on my machine, but does not work on my friend's. I have looked at his registry and the key is in the same place as mine.

I tried to put try{} catch{} blocks around OpenBaseKey to see if I can see which exception is being thrown, and none of the exceptions are being caught (Makes me think that it doesn't get past OpenBaseKey.

So, what is happening on my friend's machine, the application will run until my MessageBox.Show("Getting Registry Key") and then crash. I have included a screen shot of the error box. What could be causing this?

string path = string.Empty;
const string hfss_key_name = @"SOFTWARE\Ansoft\HFSS\2014.0\Desktop";
const string hfss_value = "LibraryDirectory";

MessageBox.Show("Getting Registry Key");

RegistryKey  localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);

localKey = localKey.OpenSubKey(hfss_key_name);
if(localKey != null)
{
    path = localKey.GetValue(hfss_value).ToString();
    path += @"\HFSS\userlib\";
}

Error Window:

enter image description here

I found the issue, I had the wrong hfss_value. For some reason his and my value are different for that LibraryDirectory. Thanks for all the help.

4

There are 4 answers

3
Martin Liversage On BEST ANSWER

As it has been pointed out you need to catch the exception to determine why there is a problem. Otherwise, your application can crash exactly as you experience it.

But even without any details about the exception with some code inspection it is perhaps possible to reveal the source of the exception.

Unless you have some weird security settings reading from the registry should be possible and you are careful to check for existence of the key. However, the call to GetValue will return null if the value is missing and calling ToString on null will throw a NullReferenceException. The next line of code should not throw an exception even if path is null.

So changing this code

path = localKey.GetValue(hfss_value).ToString();
path += @"\HFSS\userlib\";

into this code

var value = localKey.GetValue(hffs_value);
if (value != null)
  path = value + @"\HFSS\userlib\";

should fix the problem if I am not mistaken.

1
Theodosius Von Richthofen On

Make sure you are administrator on your friends machine. LocalMachine generally needs admin privileges. you might check out http://msdn.microsoft.com/en-us/library/windows/desktop/ms724878(v=vs.85).aspx

4
Allan Elder On

You probably don't have permissions to get to the key; it is crashing because you're not catching exception thrown.

Change your code to catch the exception and it should help you figure it out:

        string path = string.Empty;
        const string hfss_key_name = @"SOFTWARE\Ansoft\HFSS\2014.0\Desktop";
        const string hfss_value = "LibraryDirectory";

        MessageBox.Show("Getting Registry Key");

        try
        {
            RegistryKey localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);

            localKey = localKey.OpenSubKey(hfss_key_name);
            if (localKey != null)
            {
                path = localKey.GetValue(hfss_value).ToString();
                path += @"\HFSS\userlib\";
            }
        }
        catch (SecurityException secex)
        {
            MessageBox.Show("SecurityException: " + secex);
        }
        catch (UnauthorizedAccessException uex)
        {
            MessageBox.Show("UnauthorizedAccessException: " + uex);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception: " + ex);
        }
2
ChatterOne On

If you run your app with administrator privileges (right mouse button -> Run as administrator) and it works, it's a permission problem. If it still crashes, most likely it's because you have a 64 bit Windows, while he has a 32 bit Windows or vice versa.