Program.cs 7.2 KB

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