2
0

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