using Microsoft.Extensions.DependencyInjection; using ModVersionChecker.forms; using Microsoft.Extensions.Hosting; using ModVersionChecker.managers.interfaces; using ModVersionChecker.managers.filesystem; using ModVersionChecker.managers.litedb; namespace ModVersionChecker { class Program { [STAThread] static void Main() { var builder = Host.CreateDefaultBuilder(); var program = new Program(); builder.ConfigureServices(services => { services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddTransient(); services.AddTransient(); services.AddTransient(); }); using var host = builder.Build(); var lifetime = host.Services.GetRequiredService(); lifetime.ApplicationStarted.Register(() => { Console.WriteLine("Application is shutting down..."); }); Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); if (!SystemTray.IsSupported()) { MessageBox.Show("System tray not supported", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var serviceProvider = host.Services; var configForm = serviceProvider.GetService(); var versionChecker = serviceProvider.GetService(); var notifyIconService = serviceProvider.GetRequiredService(); var configManager = serviceProvider.GetRequiredService(); var config = configManager.GetConfig(); EventHandler openFormHandler = (s, e) => { if (configForm == null) return; configForm.UpdateListView(); if (configForm.Visible) { configForm.BringToFront(); return; } configForm.ShowDialog(); }; using (var notifyIcon = new NotifyIcon()) { notifyIcon.Icon = new Icon("Resources/MVC-Icon.ico"); // Place MVC-Icon.ico in Resources notifyIcon.Text = "Update Checker"; notifyIcon.Visible = true; notifyIconService.SetNotifyIcon(notifyIcon); var contextMenu = new ContextMenuStrip(); contextMenu.Items.Add("Configure", null, openFormHandler); contextMenu.Items.Add("Exit", null, (s, e) => Application.Exit()); notifyIcon.ContextMenuStrip = contextMenu; notifyIcon.DoubleClick += openFormHandler; bool checkOnInitialStart = config.CheckOnStartup; if (checkOnInitialStart && versionChecker != null) { versionChecker.StartVersionChecking(notifyIcon); versionChecker.OnFinished += (s, e) => { if (configForm != null) { if (configForm.InvokeRequired) { configForm.Invoke(() => configForm.UpdateListView()); } else { configForm.UpdateListView(); } } }; } if (versionChecker != null) { if (configForm != null) { configForm.OnRecheck += (s, e) => { if (versionChecker != null) { versionChecker.CheckAsync(); } }; } } // Add to startup // AddToStartup(); Application.Run(); // Keep app running for tray icon } host.RunAsync().GetAwaiter().GetResult(); } static void AddToStartup() { using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true)) { key?.SetValue("XintanalabsUpdateChecker", $"\"{Application.ExecutablePath}\""); } } } public class SystemTray { public static bool IsSupported() => System.Windows.Forms.SystemInformation.TerminalServerSession == false; } }