using Microsoft.Win32; using System; using System.Diagnostics; using System.IO; using System.Reflection; using System.Threading; using System.Windows; namespace FastGithub.UI { /// /// App.xaml 的交互逻辑 /// public partial class App : Application { private Mutex globalMutex; private Process fastGithub; /// /// 程序启动 /// /// protected override void OnStartup(StartupEventArgs e) { this.globalMutex = new Mutex(true, "Global\\FastGithub.UI", out var firstInstance); if (firstInstance == false) { this.Shutdown(); } else { AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve; SetWebBrowserVersion(9000); this.fastGithub = StartFastGithub(); } base.OnStartup(e); } /// /// 程序集加载失败时 /// /// /// /// private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) { var name = new AssemblyName(args.Name).Name; return name.EndsWith(".resources") ? null : LoadAssembly(name); } /// /// 从资源加载程序集 /// /// /// private static Assembly LoadAssembly(string name) { var stream = GetResourceStream(new Uri($"Resource/{name}.dll", UriKind.Relative)).Stream; var buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); return Assembly.Load(buffer); } /// /// 设置浏览器版本 /// /// private static void SetWebBrowserVersion(int version) { var registry = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true); var key = $"{Process.GetCurrentProcess().ProcessName}.exe"; registry.SetValue(key, version, RegistryValueKind.DWord); } /// /// 启动fastgithub /// /// private static Process StartFastGithub() { const string fileName = "fastgithub.exe"; if (File.Exists(fileName) == false) { return default; } var startInfo = new ProcessStartInfo { FileName = fileName, UseShellExecute = false, CreateNoWindow = true }; return Process.Start(startInfo); } /// /// 程序退出 /// /// protected override void OnExit(ExitEventArgs e) { this.globalMutex.Dispose(); if (this.fastGithub != null && this.fastGithub.HasExited == false) { this.fastGithub.Kill(); } base.OnExit(e); } } }