C# – SecurityException when using Registry.LocalMachine.OpenSubKey

cregistrySecuritywindows-vista

I'm developing an application that needs to write to the registry. It works fine on XP, but when I run it on Vista, from Visual Studio, I get a security exception in:

Registry.LocalMachine.OpenSubKey("SOFTWARE", true);

I'm trying to write a new key into that branch of the registry.

What's the right way to do this, firstly so that I can run my application from VS on Vista, and secondly so that my users don't run into problems running on Vista.

Thanks…

Best Answer

On both XP and Vista you need Admin rights to write a new key under LocalMachine.

You'll be finding this works on XP and fails on Vista due to different account defaults.

The quick and dirty solution is to ensure your application runs with Admin rights in both cases, though in on Vista this tends to be frowned upon.

The better solution would be to redesign things slightly - can the new sub key be written by your installer (which runs with Admin rights), or could you store your information somewhere else?