Trying to access a registry key using C# code, but getting an error

Trying to access a registry key using C# code, but getting an error

I tried to access the registry key LocalMachine/Software in C# using the code:
RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software",true);

But I am getting the following error:

Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Requested registry access is not allowed.

Stack Trace:

[SecurityException: Requested registry access is not allowed.]
   Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) +473
   EncryptionTestApp.Test.Button1_Click(Object sender, EventArgs e) in 
c:inetpubwwwrootencryptiontestapptest.aspx.cs:56
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostB
ackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,
String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain()
   System.Web.UI.Page.ProcessRequest()
   System.Web.UI.Page.ProcessRequest(HttpContext context)
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute()
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& 
completedSynchronously) +87

Please help!

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchWinDevelopment.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchWinDevelopment.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

The first quick hack is to run regedit and try to open the desired key. This way you discard the possiblity that your Windows user lacks the permission. Next, try the following:
new System.Security.Permissions.RegistryPermission(
 System.Security.Permissions.PermissionState.Unrestricted).Assert();
try
{
 // Access your key.
}
finally
{
 System.Security.Permissions.RegistryPermission.RevertAssert();
}

This tries to increase your assembly permissions with regards to the registry access (provided you have permissions to do that in the first place). The Assert..try..finally..Revert pattern must always be followed, to ensure you undo any security changes you make.

If this doesn't work, try signing your assembly. You can do this by first running the following command from the VS .NET command prompt:

sn –k mykey.snk

Then add the following attribute to your AssemblyInfo.cs file:

[assembly: AssemblyKeyFile(@"C:mykey.snk")]

If that doesn't work, you're probably on a corporate network where registry access is denied by enterprise .NET policy, in which case you should consult the appropriate administrator.

This was first published in September 2003