Program.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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(string[] args)
  70. {
  71. var startInfo = new ProcessStartInfo();
  72. startInfo.FileName = Application.ExecutablePath;
  73. startInfo.Arguments =string.Join(" ",args);
  74. startInfo.Verb = "runas"; // 以管理员身份运行
  75. try
  76. {
  77. Process.Start(startInfo);
  78. }
  79. catch (System.ComponentModel.Win32Exception)
  80. {
  81. // 用户取消了管理员权限提示,或者其他错误
  82. // 可以在此处处理错误情况
  83. }
  84. Application.Exit();
  85. }
  86. /// <summary>
  87. /// 应用程序的主入口点。
  88. /// </summary>
  89. [STAThread]
  90. static void Main(string[] args)
  91. {
  92. if (args.Length > 0 && args[0].Trim().ToLower()=="debug")
  93. {
  94. AppConst.Debug = true;
  95. }
  96. string outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine} {Exception}";
  97. Serilog.Log.Logger = new Serilog.LoggerConfiguration()
  98. .WriteTo.Console(outputTemplate: outputTemplate)
  99. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Information)
  100. .WriteTo.File("Logs\\Info\\.log", rollingInterval: Serilog.RollingInterval.Day))
  101. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Warning)
  102. .WriteTo.File("Logs\\Warning\\.log", rollingInterval: Serilog.RollingInterval.Day))
  103. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Error)
  104. .WriteTo.File("Logs\\Error\\.log", rollingInterval: Serilog.RollingInterval.Day, outputTemplate: outputTemplate))
  105. .CreateLogger();
  106. WindowsFormsSettings.AllowDpiScale = true;
  107. WindowsFormsSettings.AllowHoverAnimation = DevExpress.Utils.DefaultBoolean.True;
  108. WindowsFormsSettings.AllowDefaultSvgImages = DevExpress.Utils.DefaultBoolean.True;
  109. WindowsFormsSettings.AllowRoundedWindowCorners = DevExpress.Utils.DefaultBoolean.True;
  110. WindowsFormsSettings.AnimationMode = AnimationMode.EnableAll;
  111. WindowsFormsSettings.BackgroundSkinningMode = BackgroundSkinningMode.AllColors;
  112. WindowsFormsSettings.DefaultAllowHtmlDraw = true;
  113. WindowsFormsSettings.DefaultLookAndFeel.SetSkinStyle(SkinStyle.WXICompact);
  114. WindowsFormsSettings.DefaultFont = new System.Drawing.Font("微软雅黑", 10f);
  115. WindowsFormsSettings.SetPerMonitorDpiAware();
  116. if (Debugger.IsAttached)
  117. {
  118. //DevExpress23.2以上版本查看未本地化的资源
  119. DevExpress.Utils.Localization.XtraLocalizer.EnableTraceSource();
  120. }
  121. if (IsRunningAsAdmin())
  122. {
  123. string screenTitle = ConfigurationManager.AppSettings["SystemName"];
  124. string screenCompany = ConfigurationManager.AppSettings["Company"];
  125. DxHelper.WaitHelper.SetSplashTips("Tips.txt");
  126. ChsLocalizer.UseChs();
  127. DxHelper.WaitHelper.ShowSplashScreen(screenTitle, screenCompany);
  128. DxHelper.WaitHelper.UpdateSplashMessage("正在加载程序资源文件...");
  129. MainForm mainForm = new MainForm() { Text = screenTitle };
  130. DxHelper.WaitHelper.UpdateSplashMessage("正在初始化...");
  131. System.Windows.Forms.Application.Run(mainForm);
  132. }
  133. else
  134. {
  135. RestartAsAdmin(args);
  136. }
  137. }
  138. }
  139. }