using System; using System.IO; using System.Security.Principal; using System.Windows.Forms; namespace NotePad { static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { try { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); if (!Directory.Exists("Note")) Directory.CreateDirectory("Note"); if (principal.IsInRole(WindowsBuiltInRole.Administrator)) Application.Run(new NotePadForm()); else { //创建启动对象 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); // 设置运行文件 startInfo.FileName = Application.ExecutablePath; //设置启动动作,确保以管理员身份运行 startInfo.Verb = "runas"; //如果不是管理员,则启动UAC System.Diagnostics.Process.Start(startInfo); } } catch (Exception e) { Console.WriteLine(e.Message); } } } }