Program.cs 6.2 KB

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