Program.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // MessageBox.Show("打开程序");
  52. // Application.Run(new Browser());
  53. //}
  54. //else
  55. //{
  56. //System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  57. //startInfo.UseShellExecute = true;
  58. //startInfo.WorkingDirectory = Environment.CurrentDirectory;
  59. //startInfo.FileName = Application.ExecutablePath;
  60. ////设置启动动作,确保以管理员身份运行
  61. //startInfo.Verb = "runas";
  62. //try
  63. //{
  64. // MessageBox.Show("本地账户");
  65. // System.Diagnostics.Process.Start(startInfo);
  66. //}
  67. //catch(Exception ex)
  68. //{
  69. // MessageBox.Show(ex.Message);
  70. // Application.Exit();
  71. // return;
  72. //}
  73. ////退出
  74. //Application.Exit();
  75. //}
  76. //创建启动对象
  77. Application.Run(new Browser());
  78. #endregion
  79. }
  80. catch (Exception ex)
  81. {
  82. MessageBox.Show(ex.Message+ex.StackTrace);
  83. }
  84. }
  85. //处理线程的异常
  86. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  87. {
  88. string str = GetExceptionMsg(e.Exception, e.ToString());
  89. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  90. }
  91. //未处理的异常统一通过这里返回
  92. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  93. {
  94. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  95. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  96. }
  97. /// <summary>
  98. /// 生成自定义异常消息
  99. /// </summary>
  100. /// <param name="ex">异常对象</param>
  101. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  102. /// <returns>异常字符串文本</returns>
  103. static string GetExceptionMsg(Exception ex, string backStr)
  104. {
  105. StringBuilder sb = new StringBuilder();
  106. if (ex != null)
  107. {
  108. sb.AppendLine(ex.Message);
  109. //sb.AppendLine("【异常方法】:" + ex.StackTrace);
  110. }
  111. //else { sb.AppendLine("【未处理异常】:" + backStr); }
  112. return sb.ToString();
  113. }
  114. }
  115. }