Security permissions problem

J

Johnny Jörgensen

I've got a program where I call a method in an internal class to update the
registry. Something like this:

Program:

using System;
using System.Security.Permissions;
using Microsoft.Win32;

namespace MyNameSpace
{
public class Program
{
[STAThread]
static void Main(string[] args)
{
RegistryClass myClass = new RegistryClass();
myClass.UpdateRegistry();
}
}
}

RegistryClass:

using System;
using System.Security.Permissions;
using Microsoft.Win32;

namespace MyNameSpace
{
internal sealed class RegistryClass
{
internal void UpdateRegistry()
{
//Update HKEY_CURRENT_USER
}
}
}

I get an error when trying to update the registry saying that I don't have
sufficient permission. So I trird doing like the documentation to the
RegistryKey class says
(http://msdn2.microsoft.com/en-us/library/microsoft.win32.registrykey.aspx)
and changed the program code to:

using System;
using System.Security.Permissions;
using Microsoft.Win32;

namespace MyNameSpace
{
[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum,
ViewAndModify = "HKEY_CURRENT_USER")]
public class Program
{
[STAThread]
static void Main(string[] args)
{
RegistryClass myClass = new RegistryClass();
myClass.UpdateRegistry();
}
}
}

It doesn't work, however. VS complains that "assembly is not a valid
attribute location for this declaration".

Where do I put it then? I think I've tried all possible locations, but to no
avail.

Can anybody tell me what I'm doing wrong? I've done a lot of programs before
that modifies the registry, and this is the first time I've run into a
security problem, so I really don't know why it happens now when it hasn't
happened before....

Cheers,
Johnny J.
 
J

Jon Skeet [C# MVP]

I've got a program where I call a method in an internal class to update the
registry. Something like this:

It doesn't work, however. VS complains that "assembly is not a valid
attribute location for this declaration".

Where do I put it then? I think I've tried all possible locations, but to no
avail.

Put it outside the namespace declaration.
Can anybody tell me what I'm doing wrong? I've done a lot of programs before
that modifies the registry, and this is the first time I've run into a
security problem, so I really don't know why it happens now when it hasn't
happened before....

Are you running this program across the network by any chance? That's
the normal source of unexpected security issues.

Jon
 
J

Johnny Jörgensen

Stupid me. I just did:

RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"bla\bla\bla");
and not:

RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"bla\bla\bla", true);

Which makes the key readonly...

Problem solved, case closed - sorry for that...



Cheers,

/Johnny J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top