Program.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.IO;
  3. using System.Security.Principal;
  4. using System.Windows.Forms;
  5. namespace NotePad
  6. {
  7. static class Program
  8. {
  9. /// <summary>
  10. /// 应用程序的主入口点。
  11. /// </summary>
  12. [STAThread]
  13. static void Main()
  14. {
  15. try
  16. {
  17. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  18. WindowsPrincipal principal = new WindowsPrincipal(identity);
  19. if (!Directory.Exists("Note"))
  20. Directory.CreateDirectory("Note");
  21. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  22. Application.Run(new NotePadForm());
  23. else
  24. {
  25. //创建启动对象
  26. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  27. // 设置运行文件
  28. startInfo.FileName = Application.ExecutablePath;
  29. //设置启动动作,确保以管理员身份运行
  30. startInfo.Verb = "runas";
  31. //如果不是管理员,则启动UAC
  32. System.Diagnostics.Process.Start(startInfo);
  33. }
  34. }
  35. catch (Exception e)
  36. {
  37. Console.WriteLine(e.Message);
  38. }
  39. }
  40. }
  41. }