0

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.

1
  • If it works on your machine, but not on his, and you both have the same type of OS (such as 64 bit Windows or 32 bit Windows), then the problem is most likely one of permissions. Most likely, you have admin privs on your machine, and he does not. Registry calls use the local process (aka local user) privs unless you use a different authentication for your registry calls. Commented Nov 12, 2014 at 20:57

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I did this along with the try, catch blocks and none of the exceptions are being thrown.
@user2970916: If you are catching exceptions but nothing is thrown then the error occurs somewhere else in your code. There is an unhandled exception somewhere in your code and to fix the problem you need to get information about this exception (the type and stack trace). Depending on the framework you are using (Windows Forms, WPF) there are various ways to get notifications about unhandled exceptions. A good starting point is theAppDomain.UnhandledException event.
Thanks I will look into that. After doing some debug prints, I found that it gets past the call to GetValue().
1

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);
        }

4 Comments

For troubleshooting you do the right thing by doing an implicit ToString on the exception when displaying it using a message box. However, there is no need to add the exception name in front of this implicit ToString as the exception will display its own name.
I figured having the exception name displayed first would make it easier for the OP, I do agree with you otherwise.
I tried this and did not get any of the exceptions thrown.
Hmm ... does the application still crash?
0

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

1 Comment

Sorry for my ignorance, but how do I figure that out?
0

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.

2 Comments

We both have 64 bit windows.
It does not run when when right clicking and running as adminisitrator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.