Program.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using DevExpress.LookAndFeel;
  2. using DevExpress.XtraEditors;
  3. using DevExpress.XtraMap.Drawing.DirectD3D9;
  4. using Serilog;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Security.Principal;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace XdCxRhDW
  17. {
  18. internal static class Program
  19. {
  20. static Program()
  21. {
  22. AppDomain.CurrentDomain.SetData("PRIVATE_BINPATH", "AddIns;");
  23. var m = typeof(AppDomainSetup).GetMethod("UpdateContextProperty", BindingFlags.NonPublic | BindingFlags.Static);
  24. var funsion = typeof(AppDomain).GetMethod("GetFusionContext", BindingFlags.NonPublic | BindingFlags.Instance);
  25. m.Invoke(null, new object[] { funsion.Invoke(AppDomain.CurrentDomain, null), "PRIVATE_BINPATH", "AddIns;" });
  26. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  27. {
  28. var args = e.ExceptionObject as Exception;
  29. Serilog.Log.Error(args, "出现未处理的异常,程序即将退出!");
  30. DxHelper.MsgBoxHelper.ShowError("出现未处理的异常,程序即将退出!");
  31. };
  32. Application.ThreadException += (sender, e) =>
  33. {
  34. DxHelper.MsgBoxHelper.ShowError($"出现未处理的线程异常!{e.Exception.Message}");
  35. Serilog.Log.Error(e.Exception, "出现未处理的线程异常");
  36. };
  37. }
  38. static bool IsRunningAsAdmin()
  39. {
  40. bool result;
  41. try
  42. {
  43. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  44. WindowsPrincipal principal = new WindowsPrincipal(identity);
  45. result = principal.IsInRole(WindowsBuiltInRole.Administrator);
  46. }
  47. catch
  48. {
  49. result = false;
  50. }
  51. return result;
  52. }
  53. static void RestartAsAdmin()
  54. {
  55. var startInfo = new ProcessStartInfo();
  56. startInfo.FileName = Application.ExecutablePath;
  57. startInfo.Verb = "runas"; // 以管理员身份运行
  58. try
  59. {
  60. Process.Start(startInfo);
  61. }
  62. catch (System.ComponentModel.Win32Exception)
  63. {
  64. // 用户取消了管理员权限提示,或者其他错误
  65. // 可以在此处处理错误情况
  66. }
  67. Application.Exit();
  68. }
  69. /// <summary>
  70. /// 应用程序的主入口点。
  71. /// </summary>
  72. [STAThread]
  73. static void Main()
  74. {
  75. string outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine} {Exception}";
  76. Serilog.Log.Logger = new Serilog.LoggerConfiguration()
  77. .WriteTo.Console(outputTemplate: outputTemplate)
  78. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Information)
  79. .WriteTo.File("Logs\\Info\\.log", rollingInterval: Serilog.RollingInterval.Day))
  80. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Warning)
  81. .WriteTo.File("Logs\\Warning\\.log", rollingInterval: Serilog.RollingInterval.Day))
  82. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Error)
  83. .WriteTo.File("Logs\\Error\\.log", rollingInterval: Serilog.RollingInterval.Day, outputTemplate: outputTemplate))
  84. .CreateLogger();
  85. WindowsFormsSettings.AllowDpiScale = true;
  86. WindowsFormsSettings.AllowHoverAnimation = DevExpress.Utils.DefaultBoolean.True;
  87. WindowsFormsSettings.AllowDefaultSvgImages = DevExpress.Utils.DefaultBoolean.True;
  88. WindowsFormsSettings.AllowRoundedWindowCorners = DevExpress.Utils.DefaultBoolean.True;
  89. WindowsFormsSettings.AnimationMode = AnimationMode.EnableAll;
  90. WindowsFormsSettings.BackgroundSkinningMode = BackgroundSkinningMode.AllColors;
  91. WindowsFormsSettings.DefaultAllowHtmlDraw = true;
  92. WindowsFormsSettings.DefaultLookAndFeel.SetSkinStyle(SkinStyle.WXICompact);
  93. WindowsFormsSettings.DefaultFont = new System.Drawing.Font("微软雅黑", 10f);
  94. WindowsFormsSettings.SetPerMonitorDpiAware();
  95. if (Debugger.IsAttached)
  96. {
  97. //DevExpress23.2以上版本查看未本地化的资源
  98. //DevExpress.Utils.Localization.XtraLocalizer.EnableTraceSource();
  99. }
  100. if (IsRunningAsAdmin())
  101. {
  102. string screenTitle = ConfigurationManager.AppSettings["SystemName"];
  103. string screenCompany = ConfigurationManager.AppSettings["Company"];
  104. DxHelper.WaitHelper.SetSplashTips("Tips.txt");
  105. ChsLocalizer.UseChs();
  106. DxHelper.WaitHelper.ShowSplashScreen(screenTitle, screenCompany);
  107. DxHelper.WaitHelper.UpdateSplashMessage("正在加载程序资源文件...");
  108. MainForm mainForm = new MainForm() { Text = screenTitle };
  109. DxHelper.WaitHelper.UpdateSplashMessage("正在初始化...");
  110. System.Windows.Forms.Application.Run(mainForm);
  111. }
  112. else
  113. {
  114. RestartAsAdmin();
  115. }
  116. }
  117. }
  118. }