Craig Shearer
Craig Shearer

Reputation: 14531

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

Upvotes: 9

Views: 13541

Answers (5)

AVEbrahimi
AVEbrahimi

Reputation: 19184

Use Registry.CurrentUser

Upvotes: -1

Henk Holterman
Henk Holterman

Reputation: 273572

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.

Upvotes: 1

Bevan
Bevan

Reputation: 44317

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?

Upvotes: 11

Ray
Ray

Reputation: 46595

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.

Upvotes: 2

Michael
Michael

Reputation: 55435

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.

Upvotes: 8

Related Questions