using System; using System.Windows.Forms; using Microsoft.Win32; namespace Clipman { internal static class StartupRegistration { private const string RunKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Run"; private const string ValueName = "Clipman"; public static void SetEnabled(bool enabled) { using (var key = Registry.CurrentUser.OpenSubKey(RunKeyPath, true)) { if (key == null) return; if (enabled) { key.SetValue(ValueName, Quote(Application.ExecutablePath), RegistryValueKind.String); } else { key.DeleteValue(ValueName, false); } } } public static bool IsEnabled() { using (var key = Registry.CurrentUser.OpenSubKey(RunKeyPath, false)) { var value = key == null ? null : key.GetValue(ValueName) as string; return string.Equals(Unquote(value), Application.ExecutablePath, StringComparison.OrdinalIgnoreCase); } } private static string Quote(string value) { return "\"" + (value ?? string.Empty).Replace("\"", "\\\"") + "\""; } private static string Unquote(string value) { if (string.IsNullOrWhiteSpace(value)) return string.Empty; var trimmed = value.Trim(); if (trimmed.Length >= 2 && trimmed[0] == '"' && trimmed[trimmed.Length - 1] == '"') { return trimmed.Substring(1, trimmed.Length - 2); } return trimmed; } } }