1

I wrote a WinForms C# app that needs administrator privileges to work and also needs to start at computer startup (with registry).

 RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
 reg.SetValue("My app", Application.ExecutablePath.ToString());

So I tried changing the manifest to requiredAdministrator and got an error about clickOnce, which I completely didn't understand. So I tried publishing the app and installing as administrator, but then when the application starts at startup it doesn't have the administrator privileges anymore.

Anyone knows how to get administrator privileges for good?

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
marco polo
  • 159
  • 1
  • 2
  • 12
  • 1
    It sounds like your program would be a massive focus for attacks if it ever became popular since you guarantee that there's code running with Admin privileges on the desktop of every logged in user. Can you not split your program into a *service* that's set to run with enough permissions/privileges it needs to do its job and then a non-admin program that runs for each user and communicates with the service to get the (overall) jobs done? – Damien_The_Unbeliever Jan 05 '15 at 13:11
  • 1
    If your application needs to run at startup then have you considered creating a [windows service](http://msdn.microsoft.com/en-us/library/zt39148a%28v=vs.110%29.aspx) instead? – Justin Jan 05 '15 at 13:21

3 Answers3

1

You can go to the shortcut => Properties => Advanced ... => check Run as administrator.

Now you have configured your shortcut to start the application with administrator privileges.

Peter
  • 27,590
  • 8
  • 64
  • 84
0

What you need is permission elevation request. Usually it is done via manifest, or if you have just a process you can start it with elevated permissions. This gives an explanation, and this explains how to embed the manifest.

NeatNerd
  • 2,305
  • 3
  • 26
  • 49
0

You can try to use self-elevating, no sure how it will looks for auto-starting application (same as @peer answer)

// at application start
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);

// if not admin - elevate
if(!isAdmin)
{
    var info = new ProcessStartInfo(Application.ExecutablePath) { Verb = "runas" };
    Process.Start(info);
    return; // exit
}

// if we are here - application runs as admin
...
Sinatr
  • 20,892
  • 15
  • 90
  • 319