Files
Taskbar-Launcher/Services/StartupService.cs
2026-06-02 17:28:25 -04:00

69 lines
2.1 KiB
C#

using System.IO;
namespace TaskbarLauncher.Services;
public static class StartupService
{
private const string ShortcutName = "TaskbarLauncher.lnk";
public static string StartupFolder => Environment.GetFolderPath(Environment.SpecialFolder.Startup);
public static string ShortcutPath => Path.Combine(StartupFolder, ShortcutName);
public static bool IsEnabled()
{
return File.Exists(ShortcutPath);
}
public static void SetEnabled(bool enabled)
{
if (enabled)
{
CreateShortcut();
}
else
{
RemoveShortcut();
}
}
public static string? GetShortcutTarget()
{
if (!File.Exists(ShortcutPath))
{
return null;
}
Type shellType = Type.GetTypeFromProgID("WScript.Shell")
?? throw new InvalidOperationException("Windows Script Host is not available.");
dynamic shell = Activator.CreateInstance(shellType)
?? throw new InvalidOperationException("Could not create Windows Script Host shell.");
dynamic shortcut = shell.CreateShortcut(ShortcutPath);
return shortcut.TargetPath;
}
private static void CreateShortcut()
{
Directory.CreateDirectory(StartupFolder);
string executablePath = Environment.ProcessPath ?? Path.Combine(ConfigService.AppFolder, "TaskbarLauncher.exe");
Type shellType = Type.GetTypeFromProgID("WScript.Shell")
?? throw new InvalidOperationException("Windows Script Host is not available.");
dynamic shell = Activator.CreateInstance(shellType)
?? throw new InvalidOperationException("Could not create Windows Script Host shell.");
dynamic shortcut = shell.CreateShortcut(ShortcutPath);
shortcut.TargetPath = executablePath;
shortcut.WorkingDirectory = ConfigService.AppFolder;
shortcut.Description = "Start Taskbar Launcher";
shortcut.Save();
}
private static void RemoveShortcut()
{
if (File.Exists(ShortcutPath))
{
File.Delete(ShortcutPath);
}
}
}