views:

2563

answers:

4

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...

+5  A: 

Standard users, and admin's running with UAC on Vista, do not have permission to write the the local machine registry key. This would fail on XP too if you ran as standard user.

Your options are:

  • Use Registry.CurrentUser instead, if the setting is per-user.
  • Run your app as administrator
  • Loosen the ACL on the key so anyone can write - which is definitely not recommended, since any malware on the box can toast the key.
Michael
+2  A: 

You can only write to that key if you're running as administrator. So you'll need to run VS as administrator and your users will need to run the application as administrator.

My suggestion would be to see if you really need to write to LocalMachine. You can write to CurrentUser without admin rights.

Ray
+5  A: 

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?

Bevan
Yes, this is a good solution for me - I think I'll set this up during install as it's an app-wide setting.
Craig Shearer
A: 

Use Registry.CurrentUser

AVEbrahimi