using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Principal;
using System.Diagnostics;
namespace WindowsFormsApp1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (!IsAdministrator())
{
try
{
ProcessStartInfo processStartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = Application.ExecutablePath,
WorkingDirectory = Environment.CurrentDirectory,
Verb = "runas"
};
//processStartInfo.UseShellExecute = true;
//processStartInfo.FileName = Application.ExecutablePath,;
//processStartInfo.WorkingDirectory = Environment.CurrentDirectory;
//processStartInfo.Verb = "runas";
Process.Start(processStartInfo); // 관리자 권한으로 다시 실행 후 이 프로그램은 종료.
}
catch (Exception e)
{
MessageBox.Show(e.Message + ": 이 프로그램은 관리자 권한으로 실행해야 합니다.");
}
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
static bool IsAdministrator()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
if (null != identity)
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
return false;
}
}
}