Program.cs 6.3 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.Diagnostics;
  7. using System.IO;
  8. using System.Reflection;
  9. using System.Security.Principal;
  10. using System.Windows.Forms;
  11. using XdCxRhDW.Framework;
  12. using XdCxRhDW.UI.Lib;
  13. namespace XdCxRhDW
  14. {
  15. internal static class Program
  16. {
  17. static Program()
  18. {
  19. //设置私有路径
  20. Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
  21. AppDomain.CurrentDomain.SetData("PRIVATE_BINPATH", "AddIns;");
  22. var m = typeof(AppDomainSetup).GetMethod("UpdateContextProperty", BindingFlags.NonPublic | BindingFlags.Static);
  23. var funsion = typeof(AppDomain).GetMethod("GetFusionContext", BindingFlags.NonPublic | BindingFlags.Instance);
  24. m.Invoke(null, new object[] { funsion.Invoke(AppDomain.CurrentDomain, null), "PRIVATE_BINPATH", "AddIns;" });
  25. //c++dll加入环境变量
  26. string paths = Environment.GetEnvironmentVariable("PATH");
  27. var dirs = Directory.EnumerateDirectories("AddIns", "*", SearchOption.AllDirectories);
  28. List<string> list = new List<string>
  29. {
  30. Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns")
  31. };
  32. foreach (var item in dirs)
  33. {
  34. list.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, item));
  35. }
  36. Environment.SetEnvironmentVariable("PATH", $"{paths};{string.Join(";", list)}");
  37. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  38. {
  39. var ex = e.ExceptionObject as Exception;
  40. while (ex.InnerException != null)
  41. ex = ex.InnerException;
  42. XdCxRhDW.Framework.LogHelper.Error("出现未处理的异常,程序即将退出!", ex);
  43. DxHelper.MsgBoxHelper.ShowError("出现未处理的异常,程序即将退出!");
  44. };
  45. Application.ThreadException += (sender, e) =>
  46. {
  47. var ex = e.Exception;
  48. while (ex.InnerException != null)
  49. ex = ex.InnerException;
  50. DxHelper.MsgBoxHelper.ShowError($"出现未处理的线程异常!{e.Exception.Message}");
  51. LogUI.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 = AppConfigHelper.Get("SystemName", "");
  132. string screenCompany = AppConfigHelper.Get("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. }