diff --git a/src/DesktopMagic/AutoStart.ps1 b/src/DesktopMagic/AutoStart.ps1 deleted file mode 100644 index f007105..0000000 --- a/src/DesktopMagic/AutoStart.ps1 +++ /dev/null @@ -1,42 +0,0 @@ -param( - [string]$AppPath, - [switch]$Enable, - [switch]$Disable -) - -# Validate input -if ([string]::IsNullOrWhiteSpace($AppPath) -or !(Test-Path $AppPath)) { - exit 5 # Invalid app path -} - -$AppName = [System.IO.Path]::GetFileNameWithoutExtension($AppPath) -$StartupPath = [Environment]::GetFolderPath('Startup') -$ShortcutPath = Join-Path $StartupPath "$AppName.lnk" - -try { - if ($Enable) { - if (Test-Path $ShortcutPath) { - exit 2 # Already enabled - } - - $WshShell = New-Object -ComObject WScript.Shell - $Shortcut = $WshShell.CreateShortcut($ShortcutPath) - $Shortcut.TargetPath = $AppPath - $Shortcut.Save() - exit 0 # Success - } - elseif ($Disable) { - if (!(Test-Path $ShortcutPath)) { - exit 3 # Already disabled - } - - Remove-Item $ShortcutPath -Force - exit 0 # Success - } - else { - exit 1 # Invalid parameters - } -} -catch { - exit 4 # Unexpected error -} \ No newline at end of file diff --git a/src/DesktopMagic/DataContexts/MainWindowDataContext.cs b/src/DesktopMagic/DataContexts/MainWindowDataContext.cs index 725f55f..183d23b 100644 --- a/src/DesktopMagic/DataContexts/MainWindowDataContext.cs +++ b/src/DesktopMagic/DataContexts/MainWindowDataContext.cs @@ -1,4 +1,5 @@ -using DesktopMagic.Settings; +using DesktopMagic.Helpers; +using DesktopMagic.Settings; using System.ComponentModel; using System.Runtime.CompilerServices; @@ -32,6 +33,23 @@ internal class MainWindowDataContext : INotifyPropertyChanged } } + public bool IsAutoStartEnabled + { + get => StartupManager.IsAutoStartEnabled(); + set + { + if (value) + { + _ = StartupManager.EnableAutoStart(); + } + else + { + _ = StartupManager.DisableAutoStart(); + } + OnPropertyChanged(); + } + } + public static DesktopMagicSettings GetSettings() { return settings; diff --git a/src/DesktopMagic/DesktopMagic.csproj b/src/DesktopMagic/DesktopMagic.csproj index 51b9acd..f66e8a5 100644 --- a/src/DesktopMagic/DesktopMagic.csproj +++ b/src/DesktopMagic/DesktopMagic.csproj @@ -34,6 +34,18 @@ + + + tlbimp + 0 + 1 + f935dc20-1cf0-11d0-adb9-00c04fd58a0b + 0 + false + true + + + Never @@ -62,4 +74,10 @@ + + + + PreserveNewest + + \ No newline at end of file diff --git a/src/DesktopMagic/Helpers/StartupManager.cs b/src/DesktopMagic/Helpers/StartupManager.cs new file mode 100644 index 0000000..77bdec8 --- /dev/null +++ b/src/DesktopMagic/Helpers/StartupManager.cs @@ -0,0 +1,82 @@ +using IWshRuntimeLibrary; + +using System; +using System.IO; +using System.Reflection; +using System.Windows; + +namespace DesktopMagic.Helpers; + +public enum AutoStartStatus +{ + Success = 0, + InvalidParameters = 1, + AlreadyEnabled = 2, + AlreadyDisabled = 3, + UnexpectedError = 4 +} + +public static class StartupManager +{ + private static readonly string appPath = Path.ChangeExtension(Assembly.GetEntryAssembly()?.Location ?? throw new InvalidOperationException("Failed to get the application path."), ".exe"); + private static readonly string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup); + private static readonly string shortcutPath = Path.Combine(startupFolder, Path.GetFileNameWithoutExtension(appPath) + ".lnk"); + + public static bool IsAutoStartEnabled() + { + return System.IO.File.Exists(shortcutPath); + } + + public static AutoStartStatus EnableAutoStart() + { + if (IsAutoStartEnabled()) + { + return AutoStartStatus.AlreadyEnabled; + } + + try + { + WshShell shell = new WshShell(); + IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath); + shortcut.TargetPath = appPath; + shortcut.WorkingDirectory = Path.GetDirectoryName(appPath); + shortcut.Save(); + return AutoStartStatus.Success; + } + catch + { + return AutoStartStatus.UnexpectedError; + } + } + + public static AutoStartStatus DisableAutoStart() + { + if (!IsAutoStartEnabled()) + { + return AutoStartStatus.AlreadyDisabled; + } + + try + { + System.IO.File.Delete(shortcutPath); + return AutoStartStatus.Success; + } + catch + { + return AutoStartStatus.UnexpectedError; + } + } + + public static void ToggleAutoStart() + { + AutoStartStatus status = IsAutoStartEnabled() ? DisableAutoStart() : EnableAutoStart(); + + _ = status switch + { + AutoStartStatus.Success => MessageBox.Show("Auto-start setting changed successfully."), + AutoStartStatus.AlreadyEnabled => MessageBox.Show("Auto-start is already enabled."), + AutoStartStatus.AlreadyDisabled => MessageBox.Show("Auto-start is already disabled."), + _ => MessageBox.Show("Failed to change auto-start setting."), + }; + } +} \ No newline at end of file diff --git a/src/DesktopMagic/MainWindow.xaml b/src/DesktopMagic/MainWindow.xaml index ee979da..94d78db 100644 --- a/src/DesktopMagic/MainWindow.xaml +++ b/src/DesktopMagic/MainWindow.xaml @@ -161,6 +161,7 @@ +