HELP! - Creating Registry with OpenSubKey Hanging

C

cr

Hello.

I have a C# (Windows forms) project that uses OpenSubKey to write some
values to a registry key. Here's the code:

// Create the Installation registry key and values
Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\Company\\Produ
ct\\1.0\\Installation");
Microsoft.Win32.RegistryKey install =
Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Company\\Product
\\1.0\\Installation", true);
install.SetValue("SERVERNAME", "ServerName");
install.SetValue("VERSION", "Version");
install.Flush();
install.Close();

For some reason on a Windows XP Pro box, the code hangs after writing the
registry values. No exceptions are thrown. On Win2k and W2k3 Server boxes,
it works just fine.

Does anyone have any idea as to why this code would hang (especially on a
WinXP Pro computer)?
Is there a better way of doing this?

Thanks.
 
T

Truong Hong Thi

I am not sure why it hangs on XP but not on other systems. However,
isn't it true that CreateSubKey returns the key, so why calling
OpenSubKey next to CreateSubKey. Because the key returned by
CreateSubKey is not closed yet, maybe that is the cause of the problem?

So I suggest you try the following:
using ( Registry install = Registry.LocalMachine.CreateSubKey(@"Your
path here"))
{
install.SetValue("SERVERNAME", "ServerName");
install.SetValue("VERSION", "Version");
}

Hope it helps,
Thi
 

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