By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,974 Members | 1,913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,974 IT Pros & Developers. It's quick & easy.

Writing to Registry permission error using RegistryKey class

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.