.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Problem in reading Registry from Windows service
 

Problem in reading Registry from Windows service

I have a win forms application the sets some value to registry.


There is a windows service too to run on the same machine with localsystem account. But when it tries to read the value from registry that the winforms application had set, it gives problem.

RegistryKey RK = objRegistry.OpenSubKey(@"Software\12_0X");

is the code that I've used and getting error(RK is null)





But if I try the code:

RegistryKey RK = objRegistry.OpenSubKey(@"Software");

it returns value..

I checked the path it is correct.. Itried the same code from winforms, it works perfectly...

Please help me in this matter...


brittogladez
brittogladez
What is objRegistry?

If objRegistry refers to HKEY_CURRENT_USER and your service and app run as different users, then that could explain why one can find the key and the other cannot.

Consider running both service and app as the same user, or store your information under HKEY_LOCAL_MACHINE instead of HKEY_CURRENT_USER. Note, however, that you generally need to be admin to write to HKEY_LOCAL_MACHINE although ordinary users can (usually) read from it.


  • Marked As Answer bybrittogladez Wednesday, August 13, 2008 4:24 AM
  •  
BinaryCoder
Thats Perfect!

Now things are up and running. I changed the registry to Local as you suggested

RegistryKey objRegistry = Registry.LocalMachine;



Thanks "BinaryCoder" Thanks MSDN.


brittogladez
  • Marked As Answer bybrittogladez Wednesday, August 13, 2008 4:24 AM
  •  
brittogladez
What is objRegistry?

If objRegistry refers to HKEY_CURRENT_USER and your service and app run as different users, then that could explain why one can find the key and the other cannot.

Consider running both service and app as the same user, or store your information under HKEY_LOCAL_MACHINE instead of HKEY_CURRENT_USER. Note, however, that you generally need to be admin to write to HKEY_LOCAL_MACHINE although ordinary users can (usually) read from it.


  • Marked As Answer bybrittogladez Wednesday, August 13, 2008 4:24 AM
  •  
BinaryCoder
Ye thats true objRegistry is HKEY_CURRENT_USER. I think this answers my question but any how I'll test this and post the result.

Thank you "BinaryCoder"
brittogladez
brittogladez
Thats Perfect!

Now things are up and running. I changed the registry to Local as you suggested

RegistryKey objRegistry = Registry.LocalMachine;



Thanks "BinaryCoder" Thanks MSDN.


brittogladez
  • Marked As Answer bybrittogladez Wednesday, August 13, 2008 4:24 AM
  •  
brittogladez

You can use google to search for other answers

Custom Search

More Threads

• how to do patchmanage using c#
• Using DirectoryEntry to manipulate local users
• How to use (add, read, change, delete) registry keys with vb.net
• Can't create users in Active Directory
• PerformanceCounter throws a SecurityException
• Deploying .net assemblies in GAC in Production
• Problem with constructor Bitmap
• .NET 2.0 Class Library Reference available offline?
• "press any key to continue"
• reading process memry .. ?? wats is wrong in the follwing code