RegistryKey.OpenSubKey fails with SecurityException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?RGF2aWQgSw==?=

    RegistryKey.OpenSubKey fails with SecurityException

    I have some C# code I am using to delete a subkey and its tree from the
    registry however I am running into some issues.

    At first, I was using the Registry.LocalM achine.DeleteSu bKeyTree method and
    passing it a string like this:

    SubKeyBase = @"SYSTEM\Curren tControlSet\Enu m\";
    Registry.LocalM achine.DeleteSu bKeyTree(SubKey Base + InstanceID);

    The variable InstanceID had something like this in it:

    USB\VID_xxxx&PI D_xxxx\xxxxxxxx xxxxx

    This is the instance ID of a USB device and it correlates exactly to the
    information stored about the device in the registry. By appending these two,
    I came up with the path to the registry key/tree that I wanted to delete:

    HKEY_LOCAL_MACH INE\SYSTEM\Curr entControlSet\E num\USB\VID_xxx x&PID_xxxx\xxxx xxxxxxxxx

    Using this approach, I was getting an ArgumentExcepti on saying that the key
    does not exist, however I could open up regedit and the key was there and I
    could delete it manually if I wanted without a problem. So since I couldn't
    figure out what was going on, I decided to use OpenSubKey to open the path
    defined by SubKeyBase (ie. "SYSTEM\Current ControlSet\Enum ") and then use that
    RegistryKey object to delete the key/tree I needed and see if I got any
    different result.

    RegistryKey rk = Registry.LocalM achine.OpenSubK ey(SubKeyBase, true);

    (The 2nd argument with a value of true is to indicate I need write access)

    Well, this statement is throwing a SecurityExcepti on even though I can
    manually open up regedit and delete the key. I am a fairly new to the .NET
    framework so I am not sure what I need to give my application the security
    clearance to perform these operations. I was under the impression that it
    automatically inherited the security profile of the user running the
    application but it seems this is not the case.

    Any help is greatly appreciated!

    Regards,

    David K
  • Family Tree Mike

    #2
    Re: RegistryKey.Ope nSubKey fails with SecurityExcepti on

    By chance, are you using a 64 bit OS? Something similar drove me crazy
    until I read about registry virtualization under such OSs.

    "David K" <DavidK@discuss ions.microsoft. comwrote in message
    news:B571E36D-91B1-4A54-A3C8-7DCDE94301DD@mi crosoft.com...
    >I have some C# code I am using to delete a subkey and its tree from the
    registry however I am running into some issues.
    >
    At first, I was using the Registry.LocalM achine.DeleteSu bKeyTree method
    and
    passing it a string like this:
    >
    SubKeyBase = @"SYSTEM\Curren tControlSet\Enu m\";
    Registry.LocalM achine.DeleteSu bKeyTree(SubKey Base + InstanceID);
    >
    The variable InstanceID had something like this in it:
    >
    USB\VID_xxxx&PI D_xxxx\xxxxxxxx xxxxx
    >
    This is the instance ID of a USB device and it correlates exactly to the
    information stored about the device in the registry. By appending these
    two,
    I came up with the path to the registry key/tree that I wanted to delete:
    >
    HKEY_LOCAL_MACH INE\SYSTEM\Curr entControlSet\E num\USB\VID_xxx x&PID_xxxx\xxxx xxxxxxxxx
    >
    Using this approach, I was getting an ArgumentExcepti on saying that the
    key
    does not exist, however I could open up regedit and the key was there and
    I
    could delete it manually if I wanted without a problem. So since I
    couldn't
    figure out what was going on, I decided to use OpenSubKey to open the path
    defined by SubKeyBase (ie. "SYSTEM\Current ControlSet\Enum ") and then use
    that
    RegistryKey object to delete the key/tree I needed and see if I got any
    different result.
    >
    RegistryKey rk = Registry.LocalM achine.OpenSubK ey(SubKeyBase, true);
    >
    (The 2nd argument with a value of true is to indicate I need write access)
    >
    Well, this statement is throwing a SecurityExcepti on even though I can
    manually open up regedit and delete the key. I am a fairly new to the
    .NET
    framework so I am not sure what I need to give my application the security
    clearance to perform these operations. I was under the impression that it
    automatically inherited the security profile of the user running the
    application but it seems this is not the case.
    >
    Any help is greatly appreciated!
    >
    Regards,
    >
    David K

    Comment

    • =?Utf-8?B?RGF2aWQgSw==?=

      #3
      Re: RegistryKey.Ope nSubKey fails with SecurityExcepti on

      Sorry, I entirely forgot to mention the OS it's on. This is under Windows XP
      Pro, so not a 64-bit OS. Any other ideas?

      "Family Tree Mike" wrote:
      By chance, are you using a 64 bit OS? Something similar drove me crazy
      until I read about registry virtualization under such OSs.
      >
      "David K" <DavidK@discuss ions.microsoft. comwrote in message
      news:B571E36D-91B1-4A54-A3C8-7DCDE94301DD@mi crosoft.com...
      I have some C# code I am using to delete a subkey and its tree from the
      registry however I am running into some issues.

      At first, I was using the Registry.LocalM achine.DeleteSu bKeyTree method
      and
      passing it a string like this:

      SubKeyBase = @"SYSTEM\Curren tControlSet\Enu m\";
      Registry.LocalM achine.DeleteSu bKeyTree(SubKey Base + InstanceID);

      The variable InstanceID had something like this in it:

      USB\VID_xxxx&PI D_xxxx\xxxxxxxx xxxxx

      This is the instance ID of a USB device and it correlates exactly to the
      information stored about the device in the registry. By appending these
      two,
      I came up with the path to the registry key/tree that I wanted to delete:

      HKEY_LOCAL_MACH INE\SYSTEM\Curr entControlSet\E num\USB\VID_xxx x&PID_xxxx\xxxx xxxxxxxxx

      Using this approach, I was getting an ArgumentExcepti on saying that the
      key
      does not exist, however I could open up regedit and the key was there and
      I
      could delete it manually if I wanted without a problem. So since I
      couldn't
      figure out what was going on, I decided to use OpenSubKey to open the path
      defined by SubKeyBase (ie. "SYSTEM\Current ControlSet\Enum ") and then use
      that
      RegistryKey object to delete the key/tree I needed and see if I got any
      different result.

      RegistryKey rk = Registry.LocalM achine.OpenSubK ey(SubKeyBase, true);

      (The 2nd argument with a value of true is to indicate I need write access)

      Well, this statement is throwing a SecurityExcepti on even though I can
      manually open up regedit and delete the key. I am a fairly new to the
      .NET
      framework so I am not sure what I need to give my application the security
      clearance to perform these operations. I was under the impression that it
      automatically inherited the security profile of the user running the
      application but it seems this is not the case.

      Any help is greatly appreciated!

      Regards,

      David K
      >

      Comment

      Working...