Program.cs 5.8 KB

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