473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.LocalMachine.DeleteSubKeyTree method and
passing it a string like this:

SubKeyBase = @"SYSTEM\CurrentControlSet\Enum\";
Registry.LocalMachine.DeleteSubKeyTree(SubKeyBase + InstanceID);

The variable InstanceID had something like this in it:

USB\VID_xxxx&PID_xxxx\xxxxxxxxxxxxx

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_MACHINE\SYSTEM\CurrentControlSet\Enum\U SB\VID_xxxx&PID_xxxx\xxxxxxxxxxxxx

Using this approach, I was getting an ArgumentException 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\CurrentControlSet\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.LocalMachine.OpenSubKey(SubKeyBase, true);

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

Well, this statement is throwing a SecurityException 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
Jun 27 '08 #1
2 5249
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" <Da****@discussions.microsoft.comwrote in message
news:B5**********************************@microsof t.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.LocalMachine.DeleteSubKeyTree method
and
passing it a string like this:

SubKeyBase = @"SYSTEM\CurrentControlSet\Enum\";
Registry.LocalMachine.DeleteSubKeyTree(SubKeyBase + InstanceID);

The variable InstanceID had something like this in it:

USB\VID_xxxx&PID_xxxx\xxxxxxxxxxxxx

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_MACHINE\SYSTEM\CurrentControlSet\Enum\U SB\VID_xxxx&PID_xxxx\xxxxxxxxxxxxx

Using this approach, I was getting an ArgumentException 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\CurrentControlSet\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.LocalMachine.OpenSubKey(SubKeyBase, true);

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

Well, this statement is throwing a SecurityException 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
Jun 27 '08 #2
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" <Da****@discussions.microsoft.comwrote in message
news:B5**********************************@microsof t.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.LocalMachine.DeleteSubKeyTree method
and
passing it a string like this:

SubKeyBase = @"SYSTEM\CurrentControlSet\Enum\";
Registry.LocalMachine.DeleteSubKeyTree(SubKeyBase + InstanceID);

The variable InstanceID had something like this in it:

USB\VID_xxxx&PID_xxxx\xxxxxxxxxxxxx

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_MACHINE\SYSTEM\CurrentControlSet\Enum\U SB\VID_xxxx&PID_xxxx\xxxxxxxxxxxxx

Using this approach, I was getting an ArgumentException 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\CurrentControlSet\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.LocalMachine.OpenSubKey(SubKeyBase, true);

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

Well, this statement is throwing a SecurityException 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
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bob | last post by:
I'm working on a Windows app that needs to write to the Registry HKLM. I keep getting a "System.UnauthorizedAccessException: Cannot write to the registry key." error when running the app. I'm...
0
by: csharp_helpwanted | last post by:
steps.. first, I'm creating a connection by mapping a network drive to the pc second, the map drive connection was successful I try to read a certian registry key/value (that is a valid reg...
0
by: g82martin | last post by:
I am using the RegistryKey class to access the registry on remote machines. I only require read access. I am able to successfully read registry keys under HKLM\Software\Microsoft\Windows...
2
by: Chakkaradeep | last post by:
Hi all, heres my code, *******//////////////////////Starts Here////////////////////////////////////********* try { RegistryKey rKey =...
4
by: Thomas | last post by:
Hi All, When i am trying run the following code i am geting the error 'Object reference not set to an instance of an object' Try Dim Reg As RegistryKey Dim Value As String Dim KeyValue As...
5
by: Steve Bottoms | last post by:
Hi, all... Trying to determine how to check the state of OpenSubKey when called. What is the return value if the requested subkey doesn't exist?? I try a GetType on the result but only get a...
2
by: auldh | last post by:
being a novice i searched the net to get help on my c# project. i'm trying to iterate, enumerate, loop through a registry tree hive. for example i have a sub folder under HKLM\SOFTWARE that has...
4
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. Here is my problem. I have a program that will create a registry key, however it is not and here is the code and also the error that follows: ...
1
by: Judith Schmidt | last post by:
Hi i need a way to set permission on a registry key where i have got not rights to open it. But i am the owner (administrator). The following code fails on OpenSubKey() with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.