2
0

Program.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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(9000);
  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. /// <param name="version"></param>
  49. private static void SetWebBrowserVersion(int version)
  50. {
  51. var registry = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
  52. var key = $"{Process.GetCurrentProcess().ProcessName}.exe";
  53. registry.SetValue(key, version, RegistryValueKind.DWord);
  54. }
  55. /// <summary>
  56. /// 启动fastgithub
  57. /// </summary>
  58. /// <returns></returns>
  59. private static void StartFastGithub()
  60. {
  61. const string fileName = "fastgithub.exe";
  62. if (File.Exists(fileName) == false)
  63. {
  64. return;
  65. }
  66. var startInfo = new ProcessStartInfo
  67. {
  68. FileName = fileName,
  69. Arguments = $"ParentProcessId={Process.GetCurrentProcess().Id} UdpLoggerPort={UdpLogger.Port}",
  70. UseShellExecute = false,
  71. CreateNoWindow = true
  72. };
  73. Process.Start(startInfo);
  74. }
  75. }
  76. }