Program.cs 5.4 KB

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