diff --git a/Wox.Core/UserSettings/Settings.cs b/Wox.Core/UserSettings/Settings.cs index 37b5f28fb1..2487fcb2f8 100644 --- a/Wox.Core/UserSettings/Settings.cs +++ b/Wox.Core/UserSettings/Settings.cs @@ -43,7 +43,7 @@ namespace Wox.Core.UserSettings public bool DontPromptUpdateMsg { get; set; } public bool EnableUpdateLog { get; set; } - public bool StartWoxOnSystemStartup { get; set; } + public bool StartWoxOnSystemStartup { get; set; } = true; public bool HideOnStartup { get; set; } public bool LeaveCmdOpen { get; set; } public bool HideWhenDeactive { get; set; } diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 8e121f808e..ca20f39d2e 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -58,12 +58,24 @@ namespace Wox RegisterExitEvents(); + AutoStartup(); AutoUpdates(); window.Show(); }); } + private void AutoStartup() + { + if (_settings.StartWoxOnSystemStartup) + { + if (!SettingWindow.StartupSet()) + { + SettingWindow.SetStartup(); + } + } + } + private void AutoUpdates() { if (_settings.AutoUpdates) diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index 30db56d910..31343b777a 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -30,6 +30,7 @@ namespace Wox { public partial class SettingWindow { + private const string StartupPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; public readonly IPublicAPI _api; bool settingsLoaded; private Dictionary featureControls = new Dictionary(); @@ -117,7 +118,7 @@ namespace Wox }; - cbStartWithWindows.IsChecked = CheckApplicationIsStartupWithWindow(); + cbStartWithWindows.IsChecked = _settings.StartWoxOnSystemStartup; comboMaxResultsToShow.SelectionChanged += (o, e) => { _settings.MaxResultsToShow = (int)comboMaxResultsToShow.SelectedItem; @@ -227,43 +228,47 @@ namespace Wox private void CbStartWithWindows_OnChecked(object sender, RoutedEventArgs e) { - AddApplicationToStartup(); + SetStartup(); _settings.StartWoxOnSystemStartup = true; } private void CbStartWithWindows_OnUnchecked(object sender, RoutedEventArgs e) { - RemoveApplicationFromStartup(); + RemoveStartup(); _settings.StartWoxOnSystemStartup = false; } - private void AddApplicationToStartup() + public static void SetStartup() { - using ( - RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", - true)) + using (var key = Registry.CurrentUser.OpenSubKey(StartupPath, true)) { - key.SetValue("Wox", "\"" + Infrastructure.Wox.ProgramPath + "\" --hidestart"); + var executable = Path.Combine(Infrastructure.Wox.ProgramPath, Infrastructure.Wox.Name + ".exe"); + key?.SetValue(Infrastructure.Wox.Name, executable); } } - private void RemoveApplicationFromStartup() + private void RemoveStartup() { - using ( - RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", - true)) + using (var key = Registry.CurrentUser.OpenSubKey(StartupPath, true)) { - key.DeleteValue("Wox", false); + key?.DeleteValue(Infrastructure.Wox.Name, false); } } - private bool CheckApplicationIsStartupWithWindow() + public static bool StartupSet() { - using ( - RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", - true)) + using (var key = Registry.CurrentUser.OpenSubKey(StartupPath, true)) { - return key.GetValue("Wox") != null; + var path = key?.GetValue("Wox") as string; + if (path != null) + { + var executable = Path.Combine(Infrastructure.Wox.ProgramPath, Infrastructure.Wox.Name + ".exe"); + return path == executable; + } + else + { + return false; + } } }