468,512 Members | 1,408 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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 2880
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by vbMark | last post: by
5 posts views Thread by Tony | last post: by
reply views Thread by g82martin | last post: by
4 posts views Thread by James | last post: by
8 posts views Thread by Al Kaufman | last post: by
3 posts views Thread by JR | last post: by
6 posts views Thread by stephen coleman | last post: by
1 post views Thread by Steve Bostedor | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.