App.xaml.cs 3.2 KB

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