Writing to Registry permission error using RegistryKey class

B

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\\AssemblyFolders";
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
 
N

Nicholas Paldino [.NET/C# MVP]

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?
 
B

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 said:
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]
- (e-mail address removed)

Bob said:
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;
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
 
B

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/d...tskHowToDetermineWhetherRegistryKeyExists.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 said:
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]
- (e-mail address removed)

Bob said:
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;
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
 
R

Rob Teixeira [MVP]

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]
 
B

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
 
M

mikeb

Bob said:
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 ("").
 
R

Rob Teixeira [MVP]

thisKey.SetValue(String.Empty, "blahblah")

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

-Rob Teixeira [MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top