Program.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Security.Principal;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace CpuCgServer
  12. {
  13. internal static class Program
  14. {
  15. static Program()
  16. {
  17. //设置私有路径
  18. Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
  19. AppDomain.CurrentDomain.SetData("PRIVATE_BINPATH", "AddIns;");
  20. var m = typeof(AppDomainSetup).GetMethod("UpdateContextProperty", BindingFlags.NonPublic | BindingFlags.Static);
  21. var funsion = typeof(AppDomain).GetMethod("GetFusionContext", BindingFlags.NonPublic | BindingFlags.Instance);
  22. m.Invoke(null, new object[] { funsion.Invoke(AppDomain.CurrentDomain, null), "PRIVATE_BINPATH", "AddIns;" });
  23. //c++dll加入环境变量
  24. string paths = Environment.GetEnvironmentVariable("PATH");
  25. var dirs = Directory.EnumerateDirectories("AddIns", "*", SearchOption.AllDirectories);
  26. List<string> list = new List<string>
  27. {
  28. Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns")
  29. };
  30. foreach (var item in dirs)
  31. {
  32. list.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, item));
  33. }
  34. Environment.SetEnvironmentVariable("PATH", $"{paths};{string.Join(";", list)}");
  35. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  36. {
  37. var args = e.ExceptionObject as Exception;
  38. XdCxRhDW.Framework.LogHelper.Error("出现未处理的异常,程序即将退出!", args);
  39. };
  40. Application.ThreadException += (sender, e) =>
  41. {
  42. XdCxRhDW.UI.Lib.LogUI.Error("出现未处理的线程异常!", e.Exception).Wait(5000);
  43. };
  44. AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
  45. {
  46. //if (args.Name.EndsWith(".dll"))
  47. // return Assembly.Load(args.Name);
  48. // 1. 提取程序集简单名称
  49. string assemblyName = new AssemblyName(args.Name).Name;
  50. // 2. 定义程序集可能的查找路径
  51. string assemblyPath1 = Path.Combine(Application.StartupPath, "AddIns", $"{assemblyName}.dll");
  52. string assemblyPath2 = Path.Combine(Application.StartupPath, $"{assemblyName}.dll");
  53. if (File.Exists(assemblyPath1))
  54. return Assembly.LoadFrom(assemblyPath1);
  55. if (File.Exists(assemblyPath2))
  56. return Assembly.LoadFrom(assemblyPath2);
  57. return null;
  58. };
  59. }
  60. static bool IsRunningAsAdmin()
  61. {
  62. bool result;
  63. try
  64. {
  65. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  66. WindowsPrincipal principal = new WindowsPrincipal(identity);
  67. result = principal.IsInRole(WindowsBuiltInRole.Administrator);
  68. }
  69. catch
  70. {
  71. result = false;
  72. }
  73. return result;
  74. }
  75. static void RestartAsAdmin()
  76. {
  77. var startInfo = new ProcessStartInfo();
  78. startInfo.FileName = Application.ExecutablePath;
  79. startInfo.Verb = "runas"; // 以管理员身份运行
  80. try
  81. {
  82. Process.Start(startInfo);
  83. }
  84. catch (System.ComponentModel.Win32Exception)
  85. {
  86. // 用户取消了管理员权限提示,或者其他错误
  87. // 可以在此处处理错误情况
  88. }
  89. Application.Exit();
  90. }
  91. /// <summary>
  92. /// 应用程序的主入口点。
  93. /// </summary>
  94. [STAThread]
  95. static void Main(string[] args)
  96. {
  97. if (IsRunningAsAdmin())
  98. {
  99. MainForm mainForm = new MainForm();
  100. if (args.Length > 0)
  101. {
  102. mainForm.platAddr = args[0].Trim();
  103. }
  104. System.Windows.Forms.Application.Run(mainForm);
  105. }
  106. else
  107. {
  108. RestartAsAdmin();
  109. }
  110. }
  111. }
  112. }