Program.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Threading;
  7. using System.Windows;
  8. namespace FastGithub.UI
  9. {
  10. class Program
  11. {
  12. [STAThread]
  13. static void Main(string[] args)
  14. {
  15. AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
  16. using var mutex = new Mutex(true, "Global\\FastGithub.UI", out var isFirstInstance);
  17. if (isFirstInstance == false)
  18. {
  19. return;
  20. }
  21. StartFastGithub();
  22. SetWebBrowserVersion();
  23. var app = new Application();
  24. app.StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
  25. app.Run();
  26. }
  27. /// <summary>
  28. /// 程序集加载失败时
  29. /// </summary>
  30. /// <param name="sender"></param>
  31. /// <param name="args"></param>
  32. /// <returns></returns>
  33. private static Assembly? OnAssemblyResolve(object sender, ResolveEventArgs args)
  34. {
  35. var name = new AssemblyName(args.Name).Name;
  36. if (name.EndsWith(".resources"))
  37. {
  38. return default;
  39. }
  40. var stream = Application.GetResourceStream(new Uri($"Resource/{name}.dll", UriKind.Relative)).Stream;
  41. var buffer = new byte[stream.Length];
  42. stream.Read(buffer, 0, buffer.Length);
  43. return Assembly.Load(buffer);
  44. }
  45. /// <summary>
  46. /// 设置浏览器版本
  47. /// </summary>
  48. private static void SetWebBrowserVersion()
  49. {
  50. const string subKey = @"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
  51. var registryKey = Registry.CurrentUser.OpenSubKey(subKey, true);
  52. if (registryKey == null)
  53. {
  54. registryKey = Registry.CurrentUser.CreateSubKey(subKey);
  55. }
  56. var name = $"{Process.GetCurrentProcess().ProcessName}.exe";
  57. using var webBrowser = new System.Windows.Forms.WebBrowser();
  58. var value = int.Parse($"{webBrowser.Version.Major}000");
  59. registryKey.SetValue(name, value, RegistryValueKind.DWord);
  60. }
  61. /// <summary>
  62. /// 启动fastgithub
  63. /// </summary>
  64. /// <returns></returns>
  65. private static void StartFastGithub()
  66. {
  67. const string fileName = "fastgithub.exe";
  68. if (File.Exists(fileName) == false)
  69. {
  70. return;
  71. }
  72. var startInfo = new ProcessStartInfo
  73. {
  74. FileName = fileName,
  75. Arguments = $"ParentProcessId={Process.GetCurrentProcess().Id} UdpLoggerPort={UdpLogger.Port}",
  76. UseShellExecute = false,
  77. CreateNoWindow = true
  78. };
  79. Process.Start(startInfo);
  80. }
  81. }
  82. }