Program.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. LogHelper.Error("出现未处理的异常,程序即将退出!", args).Wait(5000);
  48. };
  49. Application.ThreadException += (sender, e) =>
  50. {
  51. LogHelper.Error("出现未处理的线程异常!", e.Exception).Wait(5000);
  52. };
  53. }
  54. static bool IsRunningAsAdmin()
  55. {
  56. bool result;
  57. try
  58. {
  59. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  60. WindowsPrincipal principal = new WindowsPrincipal(identity);
  61. result = principal.IsInRole(WindowsBuiltInRole.Administrator);
  62. }
  63. catch
  64. {
  65. result = false;
  66. }
  67. return result;
  68. }
  69. static void RestartAsAdmin()
  70. {
  71. var startInfo = new ProcessStartInfo();
  72. startInfo.FileName = Application.ExecutablePath;
  73. startInfo.Verb = "runas"; // 以管理员身份运行
  74. try
  75. {
  76. Process.Start(startInfo);
  77. }
  78. catch (System.ComponentModel.Win32Exception)
  79. {
  80. // 用户取消了管理员权限提示,或者其他错误
  81. // 可以在此处处理错误情况
  82. }
  83. Application.Exit();
  84. }
  85. //win10及以上版本管理员运行无法访问网络映射盘等,需要修改注册表并且重启设备
  86. static void CheckUACReg()
  87. {
  88. try
  89. {
  90. RegistryKey key = Registry.LocalMachine;
  91. RegistryKey system = key.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System", true);
  92. if (system == null)
  93. {
  94. system = key.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System");
  95. }
  96. object obj = system.GetValue("EnableLinkedConnections");
  97. if (obj == null || (int)obj != 1)
  98. {
  99. system.SetValue("EnableLinkedConnections", Convert.ToInt32(1), RegistryValueKind.DWord);
  100. }
  101. }
  102. catch (Exception ex)
  103. {
  104. Serilog.Log.Error(ex, "修改UAC注册表信息异常!");
  105. }
  106. }
  107. /// <summary>
  108. /// 应用程序的主入口点。
  109. /// </summary>
  110. [STAThread]
  111. static void Main()
  112. {
  113. string outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine} {Exception}";
  114. Serilog.Log.Logger = new Serilog.LoggerConfiguration()
  115. .WriteTo.Console(outputTemplate: outputTemplate)
  116. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Information)
  117. .WriteTo.File("Logs\\Info\\.log", rollingInterval: Serilog.RollingInterval.Day))
  118. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Warning)
  119. .WriteTo.File("Logs\\Warning\\.log", rollingInterval: Serilog.RollingInterval.Day))
  120. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Error)
  121. .WriteTo.File("Logs\\Error\\.log", rollingInterval: Serilog.RollingInterval.Day, outputTemplate: outputTemplate))
  122. .CreateLogger();
  123. WindowsFormsSettings.AllowDpiScale = true;
  124. WindowsFormsSettings.AllowHoverAnimation = DevExpress.Utils.DefaultBoolean.True;
  125. WindowsFormsSettings.AllowDefaultSvgImages = DevExpress.Utils.DefaultBoolean.True;
  126. WindowsFormsSettings.AllowRoundedWindowCorners = DevExpress.Utils.DefaultBoolean.True;
  127. WindowsFormsSettings.AnimationMode = AnimationMode.EnableAll;
  128. WindowsFormsSettings.BackgroundSkinningMode = BackgroundSkinningMode.AllColors;
  129. WindowsFormsSettings.DefaultAllowHtmlDraw = true;
  130. WindowsFormsSettings.DefaultLookAndFeel.SetSkinStyle(SkinStyle.WXICompact);
  131. WindowsFormsSettings.DefaultFont = new System.Drawing.Font("微软雅黑", 10f);
  132. WindowsFormsSettings.SetPerMonitorDpiAware();
  133. if (Debugger.IsAttached)
  134. {
  135. //DevExpress23.2以上版本查看未本地化的资源
  136. DevExpress.Utils.Localization.XtraLocalizer.EnableTraceSource();
  137. }
  138. if (IsRunningAsAdmin())
  139. {
  140. CheckUACReg();
  141. string screenTitle = ConfigurationManager.AppSettings["SystemName"];
  142. string screenCompany = ConfigurationManager.AppSettings["Company"];
  143. DxHelper.WaitHelper.SetSplashTips("Tips.txt");
  144. ChsLocalizer.UseChs();
  145. DxHelper.WaitHelper.ShowSplashScreen(screenTitle, screenCompany);
  146. DxHelper.WaitHelper.UpdateSplashMessage("正在加载程序资源文件...");
  147. MainForm mainForm = new MainForm() { Text = screenTitle };
  148. DxHelper.WaitHelper.UpdateSplashMessage("正在初始化...");
  149. System.Windows.Forms.Application.Run(mainForm);
  150. }
  151. else
  152. {
  153. RestartAsAdmin();
  154. }
  155. }
  156. }
  157. }