Program.cs 1.3 KB

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