App.xaml.cs 3.3 KB

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