Program.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using DevExpress.LookAndFeel;
  2. using DevExpress.XtraEditors;
  3. using Microsoft.Win32;
  4. using Serilog;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  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.Tasks;
  14. using System.Windows.Forms;
  15. namespace X3TaskServer54
  16. {
  17. internal static class Program
  18. {
  19. static Program()
  20. {
  21. //设置私有路径
  22. Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
  23. AppDomain.CurrentDomain.SetData("PRIVATE_BINPATH", "AddIns;");
  24. var m = typeof(AppDomainSetup).GetMethod("UpdateContextProperty", BindingFlags.NonPublic | BindingFlags.Static);
  25. var funsion = typeof(AppDomain).GetMethod("GetFusionContext", BindingFlags.NonPublic | BindingFlags.Instance);
  26. m.Invoke(null, new object[] { funsion.Invoke(AppDomain.CurrentDomain, null), "PRIVATE_BINPATH", "AddIns;" });
  27. //c++dll加入环境变量
  28. string paths = Environment.GetEnvironmentVariable("PATH");
  29. var dirs = Directory.EnumerateDirectories("AddIns", "*", SearchOption.AllDirectories);
  30. List<string> list = new List<string>
  31. {
  32. Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns")
  33. };
  34. foreach (var item in dirs)
  35. {
  36. list.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, item));
  37. }
  38. Environment.SetEnvironmentVariable("PATH", $"{paths};{string.Join(";", list)}");
  39. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  40. {
  41. var args = e.ExceptionObject as Exception;
  42. Serilog.Log.Error(args, "出现未处理的异常,程序即将退出!");
  43. DxHelper.MsgBoxHelper.ShowError("出现未处理的异常,程序即将退出!");
  44. };
  45. Application.ThreadException += (sender, e) =>
  46. {
  47. DxHelper.MsgBoxHelper.ShowError($"出现未处理的线程异常!{e.Exception.Message}");
  48. Serilog.Log.Error(e.Exception, "出现未处理的线程异常");
  49. };
  50. }
  51. static bool IsRunningAsAdmin()
  52. {
  53. bool result;
  54. try
  55. {
  56. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  57. WindowsPrincipal principal = new WindowsPrincipal(identity);
  58. result = principal.IsInRole(WindowsBuiltInRole.Administrator);
  59. }
  60. catch
  61. {
  62. result = false;
  63. }
  64. return result;
  65. }
  66. static void RestartAsAdmin()
  67. {
  68. var startInfo = new ProcessStartInfo();
  69. startInfo.FileName = Application.ExecutablePath;
  70. startInfo.Verb = "runas"; // 以管理员身份运行
  71. try
  72. {
  73. Process.Start(startInfo);
  74. }
  75. catch (System.ComponentModel.Win32Exception)
  76. {
  77. // 用户取消了管理员权限提示,或者其他错误
  78. // 可以在此处处理错误情况
  79. }
  80. Application.Exit();
  81. }
  82. //win10及以上版本管理员运行无法访问网络映射盘等,需要修改注册表并且重启设备
  83. static void CheckUACReg()
  84. {
  85. try
  86. {
  87. RegistryKey key = Registry.LocalMachine;
  88. RegistryKey system = key.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System", true);
  89. if (system == null)
  90. {
  91. system = key.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System");
  92. }
  93. object obj = system.GetValue("EnableLinkedConnections");
  94. if (obj == null || (int)obj != 1)
  95. {
  96. system.SetValue("EnableLinkedConnections", Convert.ToInt32(1), RegistryValueKind.DWord);
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. Serilog.Log.Error(ex, "修改UAC注册表信息异常!");
  102. }
  103. }
  104. /// <summary>
  105. /// 应用程序的主入口点。
  106. /// </summary>
  107. [STAThread]
  108. static void Main()
  109. {
  110. string outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine} {Exception}";
  111. Serilog.Log.Logger = new Serilog.LoggerConfiguration()
  112. .WriteTo.Console(outputTemplate: outputTemplate)
  113. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Information)
  114. .WriteTo.File("Logs\\Info\\.log", rollingInterval: Serilog.RollingInterval.Day))
  115. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Warning)
  116. .WriteTo.File("Logs\\Warning\\.log", rollingInterval: Serilog.RollingInterval.Day))
  117. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Error)
  118. .WriteTo.File("Logs\\Error\\.log", rollingInterval: Serilog.RollingInterval.Day, outputTemplate: outputTemplate))
  119. .CreateLogger();
  120. WindowsFormsSettings.AllowDpiScale = true;
  121. WindowsFormsSettings.AllowHoverAnimation = DevExpress.Utils.DefaultBoolean.True;
  122. WindowsFormsSettings.AllowDefaultSvgImages = DevExpress.Utils.DefaultBoolean.True;
  123. WindowsFormsSettings.AllowRoundedWindowCorners = DevExpress.Utils.DefaultBoolean.True;
  124. WindowsFormsSettings.AnimationMode = AnimationMode.EnableAll;
  125. WindowsFormsSettings.BackgroundSkinningMode = BackgroundSkinningMode.AllColors;
  126. WindowsFormsSettings.DefaultAllowHtmlDraw = true;
  127. WindowsFormsSettings.DefaultLookAndFeel.SetSkinStyle(SkinStyle.WXICompact);
  128. WindowsFormsSettings.DefaultFont = new System.Drawing.Font("微软雅黑", 10f);
  129. WindowsFormsSettings.SetPerMonitorDpiAware();
  130. if (Debugger.IsAttached)
  131. {
  132. //DevExpress23.2以上版本查看未本地化的资源
  133. //DevExpress.Utils.Localization.XtraLocalizer.EnableTraceSource();
  134. }
  135. if (IsRunningAsAdmin())
  136. {
  137. XdCxRhDW.ChsLocalizer.UseChs();
  138. MainForm mainForm = new MainForm();
  139. System.Windows.Forms.Application.Run(mainForm);
  140. }
  141. else
  142. {
  143. RestartAsAdmin();
  144. }
  145. }
  146. }
  147. }