简体   繁体   English

从.Net应用程序访问注册表项失败

[英]Accessing a Registry Key is Failing from .Net Application

I am trying to access a registry key in LocalMachine. 我正在尝试访问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. 我尝试在OpenBaseKey周围放置try {} catch {}块,以查看是否可以看到抛出了哪个异常,并且没有捕获到任何异常(使我认为它不会通过OpenBaseKey。

So, what is happening on my friend's machine, the application will run until my MessageBox.Show("Getting Registry Key") and then crash. 因此,在我朋友的计算机上发生的情况下,该应用程序将一直运行到我的MessageBox.Show(“ Getting Registry Key”),然后崩溃。 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: 错误窗口:

在此处输入图片说明

I found the issue, I had the wrong hfss_value. 我发现了问题,我的hfss_value错误。 For some reason his and my value are different for that LibraryDirectory. 由于某种原因,他和我的价值对于该LibraryDirectory是不同的。 Thanks for all the help. 感谢您的所有帮助。

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 . 但是,如果缺少该值,则对GetValue的调用将返回null ,而对null调用ToString将引发NullReferenceException The next line of code should not throw an exception even if path is null . 即使pathnull ,下一行代码也不应引发异常。

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. 如果我没记错的话,应该可以解决问题。

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

Make sure you are administrator on your friends machine. 确保您是朋友计算机上的管理员。 LocalMachine generally needs admin privileges. LocalMachine通常需要管理员权限。 you might check out http://msdn.microsoft.com/en-us/library/windows/desktop/ms724878(v=vs.85).aspx 您可以查看http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms724878(v=vs.85).aspx

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. 如果仍然崩溃,则很可能是因为您使用的是64位Windows,而他使用的是32位Windows,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM