none
When calling OpenSubKey from C# application, getting Security Exception error and the application is not launching RRS feed

  • Question

  • Hello All,

    When calling OpenSubKey Registry function from the C# application, getting the below exception and because of this application is not launching.

    Application: EMR.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.Security.SecurityException at System.ThrowHelper.ThrowSecurityException(System.ExceptionResource) at Microsoft.Win32.RegistryKey.OpenSubKey(System.String, Boolean)

    Below is the Registry key value passing into the openSubkey
    HKEY_LOCAL_MACHINE\SOFTWARE\EMR\4.01.00\Technical Data

    And one more thing is, after installation first time I am able to launch the application. But Once restart the machine then when trying to launch I am seeing this error.

    Below is the code snippet:

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

    Please help me why it is throwing security exception only after restart?
    Sunday, March 3, 2019 5:11 PM

Answers

  • Hi,

    After installing it was launching by the installer (which was running as an administrator) and calling OpenSubKey in the C# executable . After reboot also it is calling by the installer but OpenSubKey is throwing Security exception in the C# executable.

    Hello,

    Many believe that attempting this type of operation should work in admin mode yet as stated before that is not the case as it's very possible the core issue is that one or more policies prevent this action.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, March 5, 2019 11:11 AM
    Moderator

All replies

  • Hello,

    With the information supplied (not knowing if this is a customer machine or your machine) it's possible the user has no permissions to read from HKLM, for example my company denies read and write operations without an registered certificate and not by any end user.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 3, 2019 7:02 PM
    Moderator
  • maybe its just about the information and distribute to the machine what you use it so user has no permissions to read from HKLM
    Sunday, March 3, 2019 7:03 PM
  • Hi,

    After installing it was launching by the installer (which was running as an administrator) and calling OpenSubKey in the C# executable . After reboot also it is calling by the installer but OpenSubKey is throwing Security exception in the C# executable.

    Tuesday, March 5, 2019 5:08 AM
  • Hi,

    After installing it was launching by the installer (which was running as an administrator) and calling OpenSubKey in the C# executable . After reboot also it is calling by the installer but OpenSubKey is throwing Security exception in the C# executable.

    Hello,

    Many believe that attempting this type of operation should work in admin mode yet as stated before that is not the case as it's very possible the core issue is that one or more policies prevent this action.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, March 5, 2019 11:11 AM
    Moderator