Program.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using HXX.Scanner.Common;
  8. namespace HXX.Scanner.Client
  9. {
  10. static class Program
  11. {
  12. /// <summary>
  13. /// 应用程序的主入口点。
  14. /// </summary>
  15. [STAThread]
  16. static void Main(string[] Args)
  17. {
  18. /**
  19. * 当前用户是管理员的时候,直接启动应用程序
  20. * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
  21. */
  22. //获得当前登录的Windows用户标示
  23. System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
  24. //创建Windows用户主题
  25. Application.EnableVisualStyles();
  26. System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
  27. //判断当前登录用户是否为管理员
  28. if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
  29. {
  30. //如果是管理员,则直接运行
  31. Application.EnableVisualStyles();
  32. Application.SetCompatibleTextRenderingDefault(false);
  33. CustomExceptionHandlerForClient ExceptionHandler = new CustomExceptionHandlerForClient();
  34. Mutex mutex = new Mutex(true, "HXX_Scanner_ZJ", out bool CreatedNew);
  35. if (!CreatedNew)
  36. {
  37. MsgManager.Warn("程序已经启动!");
  38. }
  39. else
  40. {
  41. try
  42. {
  43. Application.Run(new frmMain3());
  44. mutex.ReleaseMutex();
  45. }
  46. catch (Exception ee)
  47. {
  48. mutex.ReleaseMutex();
  49. }
  50. }
  51. }
  52. else
  53. {
  54. //创建启动对象
  55. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  56. //设置运行文件
  57. startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
  58. //设置启动参数
  59. startInfo.Arguments = String.Join(" ", Args);
  60. //设置启动动作,确保以管理员身份运行
  61. startInfo.Verb = "runas";
  62. //如果不是管理员,则启动UAC
  63. System.Diagnostics.Process.Start(startInfo);
  64. //退出
  65. System.Windows.Forms.Application.Exit();
  66. }
  67. }
  68. }
  69. }