473,472 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Writing to Registry permission error using RegistryKey class

Bob
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\\AssemblyFolde rs";
RegistryKey thisKey = Registry.LocalMachine;
thisKey = thisKey.OpenSubKey(regPath);
thisKey = thisKey.CreateSubKey("MyTest"); //it fails here.
...........
}

I read about the System.Security.Permissions.RegistryPermission class but
can't figure out how it works with with the RegistryKey class. Could anyone
help?

Thanks
Bob
Nov 20 '05 #1
6 3029
Bob,

Are you running the app locally or from a network share? If you are not
running the app under full trust, then the app will have a limited
permission set, regardless of what account it is running under.

If you are running locally, then are you sure that you have
administrative rights (or rather, the account it is running under)? Since
the assembly is from the local machine, the only thing stopping you would be
if the account you are running under doesn't have rights.

What about the permissions on the registry key, perhaps they have been
modified somehow, have you checked this?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

" Bob" <bo*******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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\\AssemblyFolde rs";
RegistryKey thisKey = Registry.LocalMachine;
thisKey = thisKey.OpenSubKey(regPath);
thisKey = thisKey.CreateSubKey("MyTest"); //it fails here.
...........
}

I read about the System.Security.Permissions.RegistryPermission class but
can't figure out how it works with with the RegistryKey class. Could anyone help?

Thanks
Bob

Nov 20 '05 #2
Bob
The app is local on my computer, and I can manually add the registry key in
regedit without any issue. I simply run it from VS.NET (also tried running
it by directly clicking the exe compiled).

Where can i check whether the registry key has a particular permission
configured?

Thanks
Bob
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uw*************@tk2msftngp13.phx.gbl...
Bob,

Are you running the app locally or from a network share? If you are not running the app under full trust, then the app will have a limited
permission set, regardless of what account it is running under.

If you are running locally, then are you sure that you have
administrative rights (or rather, the account it is running under)? Since
the assembly is from the local machine, the only thing stopping you would be if the account you are running under doesn't have rights.

What about the permissions on the registry key, perhaps they have been
modified somehow, have you checked this?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

" Bob" <bo*******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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\\AssemblyFolde rs";
RegistryKey thisKey = Registry.LocalMachine;
thisKey = thisKey.OpenSubKey(regPath);
thisKey = thisKey.CreateSubKey("MyTest"); //it fails here.
...........
}

I read about the System.Security.Permissions.RegistryPermission class but can't figure out how it works with with the RegistryKey class. Could

anyone
help?

Thanks
Bob


Nov 20 '05 #3
Bob
I did a vb script program to do the same thing and I can run it on my PC
without problem. At the bottom of this article,
http://msdn.microsoft.com/library/de...yKeyExists.asp

it mentions something about code access security vs. operating system
security. I wonder if this is my problem but still don't know what to do
with it :(
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uw*************@tk2msftngp13.phx.gbl...
Bob,

Are you running the app locally or from a network share? If you are not running the app under full trust, then the app will have a limited
permission set, regardless of what account it is running under.

If you are running locally, then are you sure that you have
administrative rights (or rather, the account it is running under)? Since
the assembly is from the local machine, the only thing stopping you would be if the account you are running under doesn't have rights.

What about the permissions on the registry key, perhaps they have been
modified somehow, have you checked this?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

" Bob" <bo*******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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\\AssemblyFolde rs";
RegistryKey thisKey = Registry.LocalMachine;
thisKey = thisKey.OpenSubKey(regPath);
thisKey = thisKey.CreateSubKey("MyTest"); //it fails here.
...........
}

I read about the System.Security.Permissions.RegistryPermission class but can't figure out how it works with with the RegistryKey class. Could

anyone
help?

Thanks
Bob


Nov 20 '05 #4
Try this instead:

thisKey = thisKey.OpenSubKey(regPath, true);

if you don't pass True, the underlying code won't open the key with write
access.

-Rob Teixeira [MVP]

" Bob" <bo*******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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\\AssemblyFolde rs";
RegistryKey thisKey = Registry.LocalMachine;
thisKey = thisKey.OpenSubKey(regPath);
thisKey = thisKey.CreateSubKey("MyTest"); //it fails here.
...........
}

I read about the System.Security.Permissions.RegistryPermission class but
can't figure out how it works with with the RegistryKey class. Could anyone help?

Thanks
Bob

Nov 20 '05 #5
Bob
Ahhh, Rob, ya da the man!

Can I ask one more question. When you create a new key, it already has a
value name (Default) with data not set. How do I set value on this name?
If I do thisKey.SetValue ("(Default", "blahblah"), it adds a second
(Default) name.

Thanks a lot for the help,
Bob

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:OP*************@TK2MSFTNGP11.phx.gbl...
Try this instead:

thisKey = thisKey.OpenSubKey(regPath, true);

if you don't pass True, the underlying code won't open the key with write
access.

-Rob Teixeira [MVP]

" Bob" <bo*******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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\\AssemblyFolde rs";
RegistryKey thisKey = Registry.LocalMachine;
thisKey = thisKey.OpenSubKey(regPath);
thisKey = thisKey.CreateSubKey("MyTest"); //it fails here.
...........
}

I read about the System.Security.Permissions.RegistryPermission class but can't figure out how it works with with the RegistryKey class. Could

anyone
help?

Thanks
Bob


Nov 20 '05 #6
thisKey.SetValue(String.Empty, "blahblah")

you can also use null or Nothing (in VB) instead of string.empty. same
thing.

-Rob Teixeira [MVP]

" Bob" <bo*******@yahoo.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
Ahhh, Rob, ya da the man!

Can I ask one more question. When you create a new key, it already has a
value name (Default) with data not set. How do I set value on this name?
If I do thisKey.SetValue ("(Default", "blahblah"), it adds a second
(Default) name.

Thanks a lot for the help,
Bob

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:OP*************@TK2MSFTNGP11.phx.gbl...
Try this instead:

thisKey = thisKey.OpenSubKey(regPath, true);

if you don't pass True, the underlying code won't open the key with write
access.

-Rob Teixeira [MVP]

" Bob" <bo*******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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\\AssemblyFolde rs";
RegistryKey thisKey = Registry.LocalMachine;
thisKey = thisKey.OpenSubKey(regPath);
thisKey = thisKey.CreateSubKey("MyTest"); //it fails here. ...........
}

I read about the System.Security.Permissions.RegistryPermission class but can't figure out how it works with with the RegistryKey class. Could

anyone
help?

Thanks
Bob



Nov 20 '05 #7

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...
7
by: vbMark | last post by:
Here's my code: string strKeys; RegistryKey rk = Registry.CurrentUser; RegistryKey val = Registry.CurrentUser; string sLocation = "Software\\Microsoft\\Windows\\CurrentVersion \\Internet...
5
by: Tony | last post by:
TIA Tony
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...
4
by: James | last post by:
I am trying to read values to the subkeys under HKLM\...\Uninstall. I get to this point... RegistryKey Sftw = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,...
8
by: Al Kaufman | last post by:
I have a simple console app that uses: regSubKey = <some registry key> Dim reg As RegistryKey = Registry.ClassesRoot.OpenSubKey(regSubKey) Dim path As String path = CStr(reg.GetValue(""))
3
by: JR | last post by:
Hi, How can I delete a registry value in vb.net Jan
6
by: stephen coleman | last post by:
Hi all and thanks for reading this. I can read anywhere in the registry, but I can't write to the local machine (XP Pro, I am not the admin.). My application needs to write to the registry the...
1
by: Steve Bostedor | last post by:
I'm trying to use the RegistryKey to write a hex number into the registry as a binary type. I know see that in DotNet, the RegistryKey tries to interprate what registry type to create depending...
4
by: yxq | last post by:
Hello, I want to get the registry key permission information whether current user has the permission to delete the registry key using RegistryRights, how to do? thank you.
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
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 ...
0
muto222
php
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.