<p>I've inherited a hoary old piece of code (by hoary, I mean warty with lots of undocumented bug fixes than WTF-y) and there's one part that's giving me a bit of trouble. Here's how it connects to the remote registry to get the add/remove programs key:</p>

<pre><code>try
{
    remoteKey = RegistryKey.OpenRemoteBaseKey(
        RegistryHive.LocalMachine, addr.Value).OpenSubKey(
        "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
    return 1;
}
catch (IOException e)
{
    IOException myexception = e;
    //Console.WriteLine("{0}: {1}: {2}",
    //    e.GetType().Name, e.Message, addr.Value);
    return 2;
}
catch (UnauthorizedAccessException e)
{
    UnauthorizedAccessException myexception = e;
    //Console.WriteLine("{0}: {1}: {2}",
    //    e.GetType().Name, e.Message, addr.Value);
    return 3;
}
catch (System.Security.SecurityException e)
{
    System.Security.SecurityException myexception = e;
    //Console.WriteLine("{0}: {1}: {2}",
    //    e.GetType().Name, e.Message, addr.Value);
    return 4;
}
</code></pre>

<p>Now, I have two problems:</p>

<ul>
<li><p>I know why the IOException - if it's a non-Windows machine it'll throw that. The difference between UnauthorizedAccessException and SecurityException I'm not so clear on. Anyone got any ideas?</p></li>
<li><p>This entire bit of code was designed before anyone had thought you might not use your local logon for everything. I can't work out how you do authentication for remotely connecting to the registry, anyway, and this code looks like it's only used in one case, when it can't get this information from WMI.</p></li>
</ul>

<p>Any help with either would be great.</p>

[{'answer_id': 49176, 'author': 'Garo Yeriazarian', 'author_id': 2655, 'author_profile': 'https://Stackoverflow.com/users/2655', 'pm_score': 1, 'selected': False, 'text': '<p>You probably have to use impersonation to change the credentials of the thread that calls the remote registry methods.  See here (<a href="http://msdn.microsoft.com/en-us/library/system.security.principal.windowsimpersonationcontext.aspx" rel="nofollow noreferrer">linky</a>) for some information on MSDN.  Basically, your thread has a security context that is used to make managed and unmanaged calls.</p>\n'}, {'answer_id': 49177, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 1, 'selected': False, 'text': '<p>According to <a href="http://msdn.microsoft.com/en-us/library/z9f66s0a.aspx" rel="nofollow noreferrer">MSDN</a>, UnauthorizedAccessException is <strong>not thrown</strong> by OpenSubKey. So I think it\'s not needed.</p>\n'}, {'answer_id': 49186, 'author': 'Merus', 'author_id': 5133, 'author_profile': 'https://Stackoverflow.com/users/5133', 'pm_score': 1, 'selected': True, 'text': '<p>John\'s pointer to MSDN answered what UnauthorizedAccessException is for - it only appears when you try to access a key remotely, using OpenRemoteBaseKey.</p>\n\n<p>We\'re a little wary about changing the security context on the computer - I\'ve found a reference <a href="http://vbcity.com/forums/topic.asp?tid=43510" rel="nofollow noreferrer">here</a> about using WMI (which we\'re already using for the vast majority of the heavy lifting) to access the registry, so I might try that instead.</p>\n'}]
['https://Stackoverflow.com/questions/49166', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5133/']