Program.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using CefSharp;
  2. using System;
  3. using System.Security.Principal;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. namespace UAS_Web
  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. var setting = new CefSettings() { IgnoreCertificateErrors = true };
  28. setting.CefCommandLineArgs.Add("disable-gpu", "1");
  29. Cef.Initialize(setting);
  30. Application.EnableVisualStyles();
  31. Application.SetCompatibleTextRenderingDefault(false);
  32. //如果是管理员的身份
  33. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  34. {
  35. Application.Run(new Browser());
  36. }
  37. else
  38. {
  39. //创建启动对象
  40. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  41. // 设置运行文件
  42. startInfo.FileName = Application.ExecutablePath;
  43. //设置启动动作,确保以管理员身份运行
  44. startInfo.Verb = "runas";
  45. //如果不是管理员,则启动UAC
  46. System.Diagnostics.Process.Start(startInfo);
  47. //退出 System.Windows.Forms.Application.Exit();
  48. }
  49. #endregion
  50. }
  51. catch (Exception)
  52. {
  53. }
  54. }
  55. //处理线程的异常
  56. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  57. {
  58. string str = GetExceptionMsg(e.Exception, e.ToString());
  59. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  60. }
  61. //未处理的异常统一通过这里返回
  62. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  63. {
  64. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  65. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  66. }
  67. /// <summary>
  68. /// 生成自定义异常消息
  69. /// </summary>
  70. /// <param name="ex">异常对象</param>
  71. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  72. /// <returns>异常字符串文本</returns>
  73. static string GetExceptionMsg(Exception ex, string backStr)
  74. {
  75. StringBuilder sb = new StringBuilder();
  76. if (ex != null)
  77. {
  78. sb.AppendLine(ex.Message);
  79. //sb.AppendLine("【异常方法】:" + ex.StackTrace);
  80. }
  81. //else { sb.AppendLine("【未处理异常】:" + backStr); }
  82. return sb.ToString();
  83. }
  84. }
  85. }