C runs the program code instance as an administrator
- 2020-12-21 18:07:52
- OfStack
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace MyWebBrowser
{
static class Program
{
/// <summary>
/// The main entry point to the application.
/// </summary>
[STAThread]
static void Main()
{
// Get the currently logged in Windows Users sign
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
// Determines whether the current logged in user is an administrator
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
// If it is an administrator, run it directly
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
// Create the launch object
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
// Setup run file
startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
// Set startup action , Be sure to run as an administrator
startInfo.Verb = "runas";
// If you are not an administrator, start UAC
System.Diagnostics.Process.Start(startInfo);
// exit
System.Windows.Forms.Application.Exit();
}
}
}
}