Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SecurityException when using Registry.LocalMachine.OpenSubKey

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

like image 962
Craig Shearer Avatar asked Mar 24 '09 19:03

Craig Shearer


4 Answers

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?

like image 112
Bevan Avatar answered Nov 06 '22 13:11

Bevan


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.
like image 40
Michael Avatar answered Nov 06 '22 13:11

Michael


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.

like image 26
Ray Avatar answered Nov 06 '22 13:11

Ray


I assume it "works under XP" because everybody runs it as Admin under XP?

  • You will could try to circumvent (create the key during Setup or use a key under CurrrentUser or something).

  • You could grant the privilege to your Application during Setup. I'm afraid I'm a little short on details on how to do that.

like image 38
Henk Holterman Avatar answered Nov 06 '22 13:11

Henk Holterman