473,408 Members | 1,980 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,408 software developers and data experts.

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 16 '05 #1
7 14802
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 16 '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 16 '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 16 '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 16 '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 16 '05 #6
Bob wrote:
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.


From the docs for SetValue():

To set the default value for a particular registrykey, name can be set
to either a null reference (Nothing in Visual Basic), or the empty
string ("").
--
mikeb
Nov 16 '05 #7
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 16 '05 #8

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

Similar topics

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...
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(""))
6
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...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
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
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...

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.