Program.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. using XdCxRhDW.Framework;
  19. namespace XdCxRhDW
  20. {
  21. internal static class Program
  22. {
  23. static Program()
  24. {
  25. //设置私有路径
  26. Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
  27. AppDomain.CurrentDomain.SetData("PRIVATE_BINPATH", "AddIns;");
  28. var m = typeof(AppDomainSetup).GetMethod("UpdateContextProperty", BindingFlags.NonPublic | BindingFlags.Static);
  29. var funsion = typeof(AppDomain).GetMethod("GetFusionContext", BindingFlags.NonPublic | BindingFlags.Instance);
  30. m.Invoke(null, new object[] { funsion.Invoke(AppDomain.CurrentDomain, null), "PRIVATE_BINPATH", "AddIns;" });
  31. //c++dll加入环境变量
  32. string paths = Environment.GetEnvironmentVariable("PATH");
  33. var dirs = Directory.EnumerateDirectories("AddIns", "*", SearchOption.AllDirectories);
  34. List<string> list = new List<string>
  35. {
  36. Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns")
  37. };
  38. foreach (var item in dirs)
  39. {
  40. list.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, item));
  41. }
  42. Environment.SetEnvironmentVariable("PATH", $"{paths};{string.Join(";", list)}");
  43. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  44. {
  45. var args = e.ExceptionObject as Exception;
  46. Serilog.Log.Error(args, "出现未处理的异常,程序即将退出!");
  47. DxHelper.MsgBoxHelper.ShowError("出现未处理的异常,程序即将退出!");
  48. };
  49. Application.ThreadException += (sender, e) =>
  50. {
  51. DxHelper.MsgBoxHelper.ShowError($"出现未处理的线程异常!{e.Exception.Message}");
  52. Serilog.Log.Error(e.Exception, "出现未处理的线程异常");
  53. };
  54. }
  55. static bool IsRunningAsAdmin()
  56. {
  57. bool result;
  58. try
  59. {
  60. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  61. WindowsPrincipal principal = new WindowsPrincipal(identity);
  62. result = principal.IsInRole(WindowsBuiltInRole.Administrator);
  63. }
  64. catch
  65. {
  66. result = false;
  67. }
  68. return result;
  69. }
  70. static void RestartAsAdmin()
  71. {
  72. var startInfo = new ProcessStartInfo();
  73. startInfo.FileName = Application.ExecutablePath;
  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()
  91. {
  92. //double max = 0;
  93. //double min = 99999;
  94. //for (int i = 0; i < 1000000; i++)
  95. //{
  96. // var res = RandomHelper.Normal(0, 1);
  97. // if (max < res)
  98. // {
  99. // max = res;
  100. // }
  101. // if (min > res)
  102. // {
  103. // min = res;
  104. // }
  105. //}
  106. string outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine} {Exception}";
  107. Serilog.Log.Logger = new Serilog.LoggerConfiguration()
  108. .WriteTo.Console(outputTemplate: outputTemplate)
  109. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Information)
  110. .WriteTo.File("Logs\\Info\\.log", rollingInterval: Serilog.RollingInterval.Day))
  111. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Warning)
  112. .WriteTo.File("Logs\\Warning\\.log", rollingInterval: Serilog.RollingInterval.Day))
  113. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Error)
  114. .WriteTo.File("Logs\\Error\\.log", rollingInterval: Serilog.RollingInterval.Day, outputTemplate: outputTemplate))
  115. .CreateLogger();
  116. WindowsFormsSettings.AllowDpiScale = true;
  117. WindowsFormsSettings.AllowHoverAnimation = DevExpress.Utils.DefaultBoolean.True;
  118. WindowsFormsSettings.AllowDefaultSvgImages = DevExpress.Utils.DefaultBoolean.True;
  119. WindowsFormsSettings.AllowRoundedWindowCorners = DevExpress.Utils.DefaultBoolean.True;
  120. WindowsFormsSettings.AnimationMode = AnimationMode.EnableAll;
  121. WindowsFormsSettings.BackgroundSkinningMode = BackgroundSkinningMode.AllColors;
  122. WindowsFormsSettings.DefaultAllowHtmlDraw = true;
  123. WindowsFormsSettings.DefaultLookAndFeel.SetSkinStyle(SkinStyle.WXICompact);
  124. WindowsFormsSettings.DefaultFont = new System.Drawing.Font("微软雅黑", 10f);
  125. WindowsFormsSettings.SetPerMonitorDpiAware();
  126. if (Debugger.IsAttached)
  127. {
  128. //DevExpress23.2以上版本查看未本地化的资源
  129. DevExpress.Utils.Localization.XtraLocalizer.EnableTraceSource();
  130. }
  131. if (IsRunningAsAdmin())
  132. {
  133. string screenTitle = ConfigurationManager.AppSettings["SystemName"];
  134. string screenCompany = ConfigurationManager.AppSettings["Company"];
  135. DxHelper.WaitHelper.SetSplashTips("Tips.txt");
  136. ChsLocalizer.UseChs();
  137. DxHelper.WaitHelper.ShowSplashScreen(screenTitle, screenCompany);
  138. DxHelper.WaitHelper.UpdateSplashMessage("正在加载程序资源文件...");
  139. MainForm mainForm = new MainForm() { Text = screenTitle };
  140. DxHelper.WaitHelper.UpdateSplashMessage("正在初始化...");
  141. System.Windows.Forms.Application.Run(mainForm);
  142. }
  143. else
  144. {
  145. RestartAsAdmin();
  146. }
  147. }
  148. }
  149. }