473,796 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegistryKey.Ope nSubKey fails with SecurityExcepti on

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
Jun 27 '08 #1
2 5268
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****@discuss ions.microsoft. comwrote in message
news:B5******** *************** ***********@mic rosoft.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
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****@discuss ions.microsoft. comwrote in message
news:B5******** *************** ***********@mic rosoft.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
Jun 27 '08 #3

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

Similar topics

7
14834
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 logged in as an Administrator and it's a Windows app. What permission does it need? Here's the code. private void ChangeReg() { string regPath = "SOFTWARE\\Microsoft\\.NETFramework\\AssemblyFolders"; RegistryKey thisKey = Registry.LocalMachine;
0
2854
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 item) third, I disconnect the network drive connection it works great the first time but if i run it again on the same pc anytime after that I get the following
0
3270
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 NT\CurrentVersion\HotFix but I get a permissions error when I try to access HKLM\Software\Network Associates\TVD\Shared Components\VirusScan Engine\4.0.xx. The security permissions are identical on these two keys. Here is my code: RegistryKey...
2
5283
by: Chakkaradeep | last post by:
Hi all, heres my code, *******//////////////////////Starts Here////////////////////////////////////********* try { RegistryKey rKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\AICM\\Position",true);
4
8462
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 String = "Software\\MyCompany\\Connections" Reg = Registry.LocalMachine.OpenSubKey(KeyValue, True)
5
4673
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 bizarre error, so I'm uncertain what should be returned on both success or failure! Thanks! Jack
2
3594
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 some 21 sub folders and there are sub folders under some of he 21. i have no problem going into the sub folder (lets call it) programX. when i do an readkeys.opensubkey(regipath) and then keyvalue.getvalue(...) it will not only read the "values"...
4
1726
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: Imports System.IO Imports System.Text Imports System.Security.Cryptography Imports System.Net Imports Microsoft.VisualBasic.Conversion Imports System.Management
1
2277
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 System.Security.SecurityException: Requested registry access is not allowed // ----start---- SetPermissions(){
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10244
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10201
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.