Program.cs 6.2 KB

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