From 430ce9c13674fee74981c09573c60b2a9e2639ef Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Tue, 11 Mar 2014 21:40:25 +0800 Subject: [PATCH] Refactoring "startup with windows" option. --- Wox/SettingWindow.xaml.cs | 73 +++++++++++++-------------------------- Wox/Wox.csproj | 20 +++++++++++ 2 files changed, 44 insertions(+), 49 deletions(-) diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index e2999dbd4..9e85b6d54 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -1,24 +1,21 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Security.Permissions; using System.Windows; using System.Windows.Controls; -using System.Windows.Forms; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using Microsoft.Win32; -using Wox.Helper; +using IWshRuntimeLibrary; using Wox.Infrastructure; using Wox.Infrastructure.UserSettings; +using Application = System.Windows.Forms.Application; +using File = System.IO.File; using MessageBox = System.Windows.MessageBox; namespace Wox { public partial class SettingWidow : Window { + string woxLinkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "wox.lnk"); public MainWindow MainWindow; public SettingWidow() @@ -33,8 +30,6 @@ namespace Wox Loaded += Setting_Loaded; } - - private void Setting_Loaded(object sender, RoutedEventArgs ev) { ctlHotkey.OnHotkeyChanged += ctlHotkey_OnHotkeyChanged; @@ -60,7 +55,7 @@ namespace Wox cbReplaceWinR.IsChecked = CommonStorage.Instance.UserSetting.ReplaceWinR; webSearchView.ItemsSource = CommonStorage.Instance.UserSetting.WebSearches; lvCustomHotkey.ItemsSource = CommonStorage.Instance.UserSetting.CustomPluginHotkeys; - cbStartWithWindows.IsChecked = CommonStorage.Instance.UserSetting.StartWoxOnSystemStartup; + cbStartWithWindows.IsChecked = File.Exists(woxLinkPath); } public void ReloadWebSearchView() @@ -121,50 +116,30 @@ namespace Wox private void CbStartWithWindows_OnChecked(object sender, RoutedEventArgs e) { - if (!CommonStorage.Instance.UserSetting.StartWoxOnSystemStartup) - { - CommonStorage.Instance.UserSetting.StartWoxOnSystemStartup = true; - OnStartWithWindowsChecked(); - CommonStorage.Instance.Save(); - } + CreateStartupFolderShortcut(); } private void CbStartWithWindows_OnUnchecked(object sender, RoutedEventArgs e) { - CommonStorage.Instance.UserSetting.StartWoxOnSystemStartup = false; - OnStartWithWindowUnChecked(); - CommonStorage.Instance.Save(); - } - - [MethodImpl(MethodImplOptions.NoInlining)] - private void OnStartWithWindowUnChecked() - { - UAC.ExecuteAdminMethod(() => SetStartup(false)); - } - - [MethodImpl(MethodImplOptions.NoInlining)] - private void OnStartWithWindowsChecked() - { - UAC.ExecuteAdminMethod(() => SetStartup(true)); - } - - private void SetStartup(bool startup) - { - RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); - - if (rk != null) + if (File.Exists(woxLinkPath)) { - if (startup) - { - rk.SetValue("Wox", Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Wox.exe hidestart")); - } - else - { - rk.DeleteValue("Wox", false); - } + File.Delete(woxLinkPath); } } + private void CreateStartupFolderShortcut() + { + WshShellClass wshShell = new WshShellClass(); + + IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(woxLinkPath); + shortcut.TargetPath = Application.ExecutablePath; + shortcut.Arguments = "hideStart"; + shortcut.WorkingDirectory = Application.StartupPath; + shortcut.Description = "Launch Wox"; + shortcut.IconLocation = Application.StartupPath + @"\App.ico"; + shortcut.Save(); + } + void ctlHotkey_OnHotkeyChanged(object sender, System.EventArgs e) { if (ctlHotkey.CurrentHotkeyAvailable) @@ -192,7 +167,7 @@ namespace Wox { CustomPluginHotkey item = lvCustomHotkey.SelectedItem as CustomPluginHotkey; if (item != null && - MessageBox.Show("Are your sure to delete " + item.Hotkey + " plugin hotkey?","Delete Custom Plugin Hotkey", + MessageBox.Show("Are your sure to delete " + item.Hotkey + " plugin hotkey?", "Delete Custom Plugin Hotkey", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { CommonStorage.Instance.UserSetting.CustomPluginHotkeys.Remove(item); diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index e120aef13..ab2cbd4ef 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -290,6 +290,26 @@ + + + {F935DC20-1CF0-11D0-ADB9-00C04FD58A0B} + 1 + 0 + 0 + tlbimp + False + True + + + {50A7E9B0-70EF-11D1-B75A-00A0C90564FE} + 1 + 0 + 0 + tlbimp + False + True + +