App.xaml.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /// <summary>
  11. /// App.xaml 的交互逻辑
  12. /// </summary>
  13. public partial class App : Application
  14. {
  15. private readonly Mutex globalMutex;
  16. private readonly bool isFirstInstance;
  17. public App()
  18. {
  19. this.globalMutex = new Mutex(true, "Global\\FastGithub.UI", out this.isFirstInstance);
  20. AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
  21. }
  22. /// <summary>
  23. /// 程序集加载失败时
  24. /// </summary>
  25. /// <param name="sender"></param>
  26. /// <param name="args"></param>
  27. /// <returns></returns>
  28. private static Assembly? OnAssemblyResolve(object sender, ResolveEventArgs args)
  29. {
  30. var name = new AssemblyName(args.Name).Name;
  31. if (name.EndsWith(".resources"))
  32. {
  33. return default;
  34. }
  35. var stream = GetResourceStream(new Uri($"Resource/{name}.dll", UriKind.Relative)).Stream;
  36. var buffer = new byte[stream.Length];
  37. stream.Read(buffer, 0, buffer.Length);
  38. return Assembly.Load(buffer);
  39. }
  40. /// <summary>
  41. /// 程序启动
  42. /// </summary>
  43. /// <param name="e"></param>
  44. protected override void OnStartup(StartupEventArgs e)
  45. {
  46. if (this.isFirstInstance == false)
  47. {
  48. this.Shutdown();
  49. }
  50. else
  51. {
  52. StartFastGithub();
  53. SetWebBrowserVersion(9000);
  54. }
  55. base.OnStartup(e);
  56. }
  57. /// <summary>
  58. /// 设置浏览器版本
  59. /// </summary>
  60. /// <param name="version"></param>
  61. private static void SetWebBrowserVersion(int version)
  62. {
  63. var registry = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
  64. var key = $"{Process.GetCurrentProcess().ProcessName}.exe";
  65. registry.SetValue(key, version, RegistryValueKind.DWord);
  66. }
  67. /// <summary>
  68. /// 启动fastgithub
  69. /// </summary>
  70. /// <returns></returns>
  71. private static void StartFastGithub()
  72. {
  73. const string fileName = "fastgithub.exe";
  74. if (File.Exists(fileName) == false)
  75. {
  76. return;
  77. }
  78. var startInfo = new ProcessStartInfo
  79. {
  80. FileName = fileName,
  81. Arguments = $"ParentProcessId={Process.GetCurrentProcess().Id} UdpLoggerPort={UdpLoggerPort.Value}",
  82. UseShellExecute = false,
  83. CreateNoWindow = true
  84. };
  85. Process.Start(startInfo);
  86. }
  87. /// <summary>
  88. /// 程序退出
  89. /// </summary>
  90. /// <param name="e"></param>
  91. protected override void OnExit(ExitEventArgs e)
  92. {
  93. this.globalMutex.Dispose();
  94. base.OnExit(e);
  95. }
  96. }
  97. }