Program.cs 6.1 KB

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