Program.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using CefSharp;
  2. using System;
  3. using System.IO;
  4. using System.Security.Principal;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Xml;
  8. namespace UAS_Web
  9. {
  10. static class Program
  11. {
  12. /// <summary>
  13. /// 应用程序的主入口点。
  14. /// </summary>
  15. [STAThread]
  16. static void Main()
  17. {
  18. try
  19. {
  20. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  21. WindowsPrincipal principal = new WindowsPrincipal(identity);
  22. //设置应用程序处理异常方式:ThreadException处理
  23. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  24. //处理UI线程异常
  25. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  26. //处理非UI线程异常
  27. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  28. #region 应用程序的主入口点
  29. var setting = new CefSettings() { IgnoreCertificateErrors = true };
  30. setting.CefCommandLineArgs.Add("disable-gpu", "1");
  31. Cef.Initialize(setting);
  32. Application.EnableVisualStyles();
  33. Application.SetCompatibleTextRenderingDefault(false);
  34. FileStream fcaches = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + "cache.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  35. fcaches.Close();
  36. FileInfo info = new FileInfo(System.AppDomain.CurrentDomain.BaseDirectory + "cache.xml");
  37. if (info.Length == 0)
  38. {
  39. XmlDocument doc = new XmlDocument();
  40. //创建类型声明节点
  41. XmlNode node = doc.CreateXmlDeclaration("1.0", "utf-8", "");
  42. doc.AppendChild(node);
  43. //创建根节点
  44. XmlElement xeRoot = doc.CreateElement("cacheInfo");
  45. doc.AppendChild(xeRoot);
  46. doc.Save(System.AppDomain.CurrentDomain.BaseDirectory + "cache.xml");
  47. }
  48. //如果是管理员的身份
  49. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  50. {
  51. Application.Run(new Browser());
  52. }
  53. else
  54. {
  55. //创建启动对象
  56. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  57. // 设置运行文件
  58. startInfo.FileName = Application.ExecutablePath;
  59. //设置启动动作,确保以管理员身份运行
  60. startInfo.Verb = "runas";
  61. //如果不是管理员,则启动UAC
  62. System.Diagnostics.Process.Start(startInfo);
  63. //退出 System.Windows.Forms.Application.Exit();
  64. }
  65. #endregion
  66. }
  67. catch (Exception)
  68. {
  69. }
  70. }
  71. //处理线程的异常
  72. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  73. {
  74. string str = GetExceptionMsg(e.Exception, e.ToString());
  75. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  76. }
  77. //未处理的异常统一通过这里返回
  78. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  79. {
  80. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  81. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  82. }
  83. /// <summary>
  84. /// 生成自定义异常消息
  85. /// </summary>
  86. /// <param name="ex">异常对象</param>
  87. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  88. /// <returns>异常字符串文本</returns>
  89. static string GetExceptionMsg(Exception ex, string backStr)
  90. {
  91. StringBuilder sb = new StringBuilder();
  92. if (ex != null)
  93. {
  94. sb.AppendLine(ex.Message);
  95. //sb.AppendLine("【异常方法】:" + ex.StackTrace);
  96. }
  97. //else { sb.AppendLine("【未处理异常】:" + backStr); }
  98. return sb.ToString();
  99. }
  100. }
  101. }