Program.cs 7.1 KB

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