Program.cs 5.7 KB

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