4

I'm researching on how to add a shortcut in the windows context menu to my application. I came across this article and I tried it out. This is the code it uses to create a key in the registry.

private void btnAddMenu_Click(object sender, System.EventArgs e)
{
    RegistryKey regmenu = null;
    RegistryKey regcmd = null;
    try
    {
        regmenu = Registry.ClassesRoot.CreateSubKey(MenuName);
        if(regmenu != null)
            regmenu.SetValue("",this.txtName.Text);
        regcmd = Registry.ClassesRoot.CreateSubKey(Command);
        if(regcmd != null)
                regcmd.SetValue("",this.txtPath.Text);
    }
    catch(Exception ex)
    {
        MessageBox.Show(this,ex.ToString());
    }
    finally       
    {
        if(regmenu != null)
            regmenu.Close();
        if(regcmd != null)
            regcmd.Close();
    }        
}

The problem is if I run it through my Administrator account, it works fine. But when I do it through a different account which doesn't have admin privileges, it throws this exception.

system.unauthorizedaccessexception access to the registry key is denied

Now if I were to use this code in one of my own applications to create a shortcut in the context menu, I can't be sure every user would run it as the Administrator, right?

Is there any way in C# to escalate the user privileges when creating the registry key?

If you know any other way to add an item to the windows context menu, I'd be interested in them too.

Thank you.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Isuru
  • 30,617
  • 60
  • 187
  • 303
  • You have to create that key when the user is installing the app as an administrator. – ZippyV Apr 06 '12 at 10:18
  • That would require user interaction, right? Is there a way to do it from the code? – Isuru Apr 06 '12 at 10:21
  • This is the job of an installer. It will have the necessary rights. Doesn't require code either. Dead simple to do in VS with a Setup project. – Hans Passant Apr 06 '12 at 10:44
  • If you're target OS is Windows 7, than please check ["How to force C# App to run as administrator on Windows 7"](http://stackoverflow.com/questions/2818179/how-to-force-c-sharp-app-to-run-as-administrator-on-windows-7) thread, maybe it could help. – alex.b Apr 06 '12 at 11:32

2 Answers2

2

You could escalate your permissions much the same way installers do it. It will require user interaction, as that's the way the OS is designed (and rightly so) - you can't go around it.

zmbq
  • 38,013
  • 14
  • 101
  • 171
2

You cannot escalate permissions as such (at least I'd like to know about it, but doesn't seem possible as yet), but you need to run / start your app (embed into manifest) elevated.
Please take a look at these entries...
How do I force my .NET application to run as administrator?
Elevating process privilege programatically?
I'd suggest what comments said, running that from the setup. Or let your app run as admin from the start, or possibly jump start an elevated process from your app - when needed (e.g. running another exe of yours that has its manifest properly).

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51