Create an "Application Manifest File" and change the following line from:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
to:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
[PrincipalPermission(SecurityAction.Demand, Role = @"BUILTINAdministrators")]
public void MyMethod()
{
}
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
// Program
public static bool IsAdministrator()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
// Form1
private void Form1Shown(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
this.Run1();
this.Cursor = Cursors.Default;
}
private void Run1()
{
if (!Program.IsAdministrator())
{
// Restart and run as admin
var exeName = Process.GetCurrentProcess().MainModule.FileName;
ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
startInfo.Verb = "runas";
startInfo.Arguments = "restart";
Process.Start(startInfo);
Application.Exit();
}
}