Program.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.LogHelper.Error("出现未处理的线程异常!", e.Exception).Wait(5000);
  43. };
  44. }
  45. static bool IsRunningAsAdmin()
  46. {
  47. bool result;
  48. try
  49. {
  50. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  51. WindowsPrincipal principal = new WindowsPrincipal(identity);
  52. result = principal.IsInRole(WindowsBuiltInRole.Administrator);
  53. }
  54. catch
  55. {
  56. result = false;
  57. }
  58. return result;
  59. }
  60. static void RestartAsAdmin()
  61. {
  62. var startInfo = new ProcessStartInfo();
  63. startInfo.FileName = Application.ExecutablePath;
  64. startInfo.Verb = "runas"; // 以管理员身份运行
  65. try
  66. {
  67. Process.Start(startInfo);
  68. }
  69. catch (System.ComponentModel.Win32Exception)
  70. {
  71. // 用户取消了管理员权限提示,或者其他错误
  72. // 可以在此处处理错误情况
  73. }
  74. Application.Exit();
  75. }
  76. /// <summary>
  77. /// 应用程序的主入口点。
  78. /// </summary>
  79. [STAThread]
  80. static void Main(string[] args)
  81. {
  82. if (IsRunningAsAdmin())
  83. {
  84. MainForm mainForm = new MainForm();
  85. System.Windows.Forms.Application.Run(mainForm);
  86. }
  87. else
  88. {
  89. RestartAsAdmin();
  90. }
  91. }
  92. }
  93. }