首页 > 解决方案 > 从 C# 应用程序调用 OpenSubKey 时,出现安全异常错误并且应用程序未启动

问题描述

从 C# 应用程序调用 OpenSubKey Registry 函数时,出现以下异常并且由于此应用程序未启动。

应用程序:EMR.exe 框架版本:v4.0.30319 描述:进程因未处理的异常而终止。异常信息:Microsoft.Win32.RegistryKey.OpenSubKey(System.String, Boolean) 处 System.ThrowHelper.ThrowSecurityException(System.ExceptionResource) 处的 System.Security.SecurityException

下面是传递到 openSubkey HKEY_LOCAL_MACHINE\SOFTWARE\EMR\4.01.00\Technical Data 的注册表项值

还有一件事是,第一次安装后,我可以启动应用程序。但是一旦重新启动机器,然后在尝试启动时,我就会看到这个错误。

下面是代码片段:

public RegistryKey GetRegKeyHandle(String key)

{

key = BASE_REG_KEY + "\\" + key;

 if (MainFrame.GetUIControlObject().MGDIsAdmin()) 
  {
     MessageBox.Show("Entered to MGDIsAdmin");
     regWritable = true;
     RegistryKey rkTest = Registry.LocalMachine.OpenSubKey(key, true);

       return rkTest;
    }
     else
     {
         return Registry.LocalMachine.OpenSubKey(key);
      }
}

请帮助我为什么仅在重新启动后才引发安全异常?

标签: c#c#-4.0

解决方案


推荐阅读