Hello Folks,

I'm trying to write a program though which I can access my system registry. Both reading and writing actions have been encapsulated. Among these I have successfully done with reading a key value from my registry. But the storm arises when I make my move to write to registry. This is the code which I used for creating a key and then storing a string type value....

using Microsoft.Win32;

Private Void cmdRead_Click()
{
   RegistryKey HKCU=Registry.CurrentUser;
   RegistryKey subKeys=HKCU.OpenSubKey("Software\\VB and VBA Program Settings\\Company\\");
   [B]RegistryKey newSubKey=subKeys.CreateSubKey("Login Status");[/B]
   newSubKey.SetValue("Status","Logged In");
   newSubKey.Close();
   subKeys.Close();
   HKCU.Close();
}

I'm getting an exception in the BOLDED line. This is the exception which I'm getting :-
"UnAuthorizedAccessException"
"Can't write in to the registry key."

Experts in C# plz help me to sort out this problem....
Thanks in advance...

regards
Shouvik

Recommended Answers

All 8 Replies

My first thought is that you don't have admin rights .. Are you running Vista ?

no, this code is running on windows xp service pack2.

one more thing, i have already tried setting admin permissions (full access control) to my system account using registry editor's permission setting dialog box. but the result remains same.

any one here can help to solve this matter?

See if you can do it manually using RegEdit.
I think you will discover the problem as you navigate to the node where you are expecting to write the value.

Hello JerryShaw,

Thanks buddy for your reply.
For your kind information, I have already done with this. The key/node under which I need to create a subkey and store a string value look perfectly and in good condiiton. when I try writing to this node manually from regedit it is working without a single piece of problem. The storm is arising when I make approach to do the same kind of thing using my c# code. I don't know what is this causing for.

I have already searched in google. There I have found a good number of examples running the same syntax. But the problem remains same. I must solve it out as my project development is completely stucked for this absurd behavior of my OS.

Is there any alternate approach available?
Could you try this code on your own machine and give me a proper suggestion on what to do next?

regards
Shouvik

Here:

RegistryKey subKeys = HKCU.OpenSubKey("Software\\VB and VBA Program Settings\\Company\\", true);

choudhuryshouvi,

The code you sent earlier produced a different error on my machine.

This modified code below does run on my system. I changed the first assignment to drop the trailing backslash, and instead of using the OpenSubKey, I use the CreateSubKey. An old trick I learned way back is that the CreateSubKey WIN API call does the same thing as the OpenSubKey, except that it will also automatically create the subkey if it does not already exist.


// Jerry

private void cmdRead_Click()
        {
           RegistryKey HKCU=Registry.CurrentUser;
           RegistryKey subKeys=HKCU.CreateSubKey("Software\\VB and VBA Program Settings\\Company\\Login Status");
           subKeys.SetValue("Status", "Logged In");
           subKeys.Close();
           HKCU.Close();
        }

thanks for your replies.
i'll try that on my machine and get back to you as soon as possible.

regards
Shouvik

thanks for ur help guys......now my problem is solved.......the registry is now successfully snatched from my program....

thanks to _r0ckbaer specially.....thanks buddy your snippet really helped me much....really appreciated..

so, this thread is now closed....

regards
Shouvik

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.