Program.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_AutoUpdate
  7. {
  8. static class Program
  9. {
  10. /// <summary>
  11. /// 应用程序的主入口点。
  12. /// </summary>
  13. [STAThread]
  14. static void Main()
  15. {
  16. try
  17. {
  18. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  19. WindowsPrincipal principal = new WindowsPrincipal(identity);
  20. //设置应用程序处理异常方式:ThreadException处理
  21. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  22. //处理UI线程异常
  23. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  24. //处理非UI线程异常
  25. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  26. #region 应用程序的主入口点
  27. Application.EnableVisualStyles();
  28. Application.SetCompatibleTextRenderingDefault(false);
  29. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  30. //创建标签缓存的文件夹
  31. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  32. Application.Run(new CheckUpdateWindow());
  33. else
  34. {
  35. //创建启动对象
  36. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  37. // 设置运行文件
  38. startInfo.FileName = Application.ExecutablePath;
  39. //设置启动动作,确保以管理员身份运行
  40. startInfo.Verb = "runas";
  41. //如果不是管理员,则启动UAC
  42. System.Diagnostics.Process.Start(startInfo);
  43. }
  44. #endregion
  45. }
  46. catch (Exception ex)
  47. {
  48. MessageBox.Show(ex.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  49. }
  50. }
  51. //处理线程的异常
  52. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  53. {
  54. MessageBox.Show(e.Exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  55. }
  56. //未处理的异常统一通过这里返回
  57. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  58. {
  59. MessageBox.Show(e.ExceptionObject.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  60. }
  61. }
  62. }