아래와 같이 프로그램 처음 실행 부분에 넣어주면 관리자 권한으로 실행시킬 수 있다.
static class Program
{
/// <summary>
/// 해당 응용 프로그램의 주 진입점입니다.
/// </summary>
[STAThread]
static void Main()
{
if (IsAdministrator() == false)
{
try
{
ProcessStartInfo procInfo = new ProcessStartInfo();
procInfo.UseShellExecute = true;
procInfo.FileName = Application.ExecutablePath;
procInfo.WorkingDirectory = Environment.CurrentDirectory;
procInfo.Verb = "runas";
Process.Start(procInfo);
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message.ToString());
}
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static bool IsAdministrator()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
if (null != identity)
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
return false;
}
}