Program.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using DevExpress.LookAndFeel;
  2. using DevExpress.XtraEditors;
  3. using Microsoft.Win32;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.Diagnostics;
  9. using System.Globalization;
  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. XdCxRhDW.Framework.LogHelper.Error("出现未处理的异常,程序即将退出!", args);
  48. };
  49. Application.ThreadException += (sender, e) =>
  50. {
  51. XdCxRhDW.UI.Lib.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. XdCxRhDW.Framework.LogHelper.Error($"修改UAC注册表信息异常!", ex);
  105. }
  106. }
  107. /// <summary>
  108. /// 应用程序的主入口点。
  109. /// </summary>
  110. [STAThread]
  111. static void Main()
  112. {
  113. WindowsFormsSettings.AllowDpiScale = true;
  114. WindowsFormsSettings.AllowHoverAnimation = DevExpress.Utils.DefaultBoolean.True;
  115. WindowsFormsSettings.AllowDefaultSvgImages = DevExpress.Utils.DefaultBoolean.True;
  116. WindowsFormsSettings.AllowRoundedWindowCorners = DevExpress.Utils.DefaultBoolean.True;
  117. WindowsFormsSettings.AnimationMode = AnimationMode.EnableAll;
  118. WindowsFormsSettings.BackgroundSkinningMode = BackgroundSkinningMode.AllColors;
  119. WindowsFormsSettings.DefaultAllowHtmlDraw = true;
  120. WindowsFormsSettings.DefaultLookAndFeel.SetSkinStyle(SkinStyle.WXICompact);
  121. WindowsFormsSettings.DefaultFont = new System.Drawing.Font("微软雅黑", 10f);
  122. WindowsFormsSettings.SetPerMonitorDpiAware();
  123. if (Debugger.IsAttached)
  124. {
  125. //DevExpress23.2以上版本查看未本地化的资源
  126. DevExpress.Utils.Localization.XtraLocalizer.EnableTraceSource();
  127. }
  128. if (IsRunningAsAdmin())
  129. {
  130. CheckUACReg();
  131. string screenTitle = ConfigurationManager.AppSettings["SystemName"];
  132. string screenCompany = ConfigurationManager.AppSettings["Company"];
  133. DxHelper.WaitHelper.SetSplashTips("Tips.txt");
  134. ChsLocalizer.UseChs();
  135. DxHelper.WaitHelper.ShowSplashScreen(screenTitle, screenCompany);
  136. DxHelper.WaitHelper.UpdateSplashMessage("正在加载程序资源文件...");
  137. MainForm mainForm = new MainForm() { Text = screenTitle };
  138. DxHelper.WaitHelper.UpdateSplashMessage("正在初始化...");
  139. System.Windows.Forms.Application.Run(mainForm);
  140. }
  141. else
  142. {
  143. RestartAsAdmin();
  144. }
  145. }
  146. }
  147. }