From 6b025a24f0153161453f26c36200f3ffbe9339d0 Mon Sep 17 00:00:00 2001 From: duffyduck Date: Mon, 8 Jun 2026 13:52:34 +0200 Subject: [PATCH] Add autostart management (per-user / all-users) in settings Setup setzt den Autostart weiterhin fuer alle Benutzer (HKLM); die Task-Beschreibung im Installer stellt das jetzt klar. In den Einstellungen neuer Abschnitt "Autostart": - "Nur fuer diesen Benutzer" -> HKCU\...\Run - "Fuer alle Benutzer" -> HKLM\...\Run, nur mit Admin-Rechten aenderbar (so laesst sich der vom Setup gesetzte All-User-Autostart auch entfernen). Neuer AutostartManager kapselt Lesen/Setzen/Entfernen beider Run-Eintraege und die Admin-Pruefung. Single-Instance-Mutex verhindert weiterhin einen Doppelstart, falls beide Eintraege gesetzt sind. Co-Authored-By: Claude Opus 4.8 (1M context) --- CHANGELOG.md | 6 ++ installer/setup.iss | 2 +- .../Services/AutostartManager.cs | 76 +++++++++++++++++++ src/StarfaceOutlookSync/UI/SettingsForm.cs | 44 ++++++++++- 4 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 src/StarfaceOutlookSync/Services/AutostartManager.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index b3e5eba0..f352fec8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,12 @@ Versionsschema ist `x.x.x.x` (siehe `release.sh`). ### Hinzugefuegt +- **Autostart-Verwaltung in den Einstellungen.** Neuer Abschnitt "Autostart" mit + zwei Optionen: "Nur fuer diesen Benutzer" (HKCU) und "Fuer alle Benutzer" + (HKLM). Die Option fuer alle Benutzer ist nur mit Admin-Rechten aenderbar - + darueber laesst sich auch der vom Setup gesetzte Autostart fuer alle wieder + entfernen. Das Setup setzt den Autostart weiterhin fuer alle Benutzer (HKLM), + der Hinweis im Installer wurde entsprechend klargestellt. - **Protokoll automatisch leeren (Einstellung).** Neuer Wert "Protokoll auto-leeren - Eintraege aelter als (Tage)". Bei 0 (Standard) bleibt alles erhalten; bei >0 werden aeltere Eintraege automatisch entfernt (beim Start, diff --git a/installer/setup.iss b/installer/setup.iss index 1f5c3255..1f17c695 100644 --- a/installer/setup.iss +++ b/installer/setup.iss @@ -32,7 +32,7 @@ Name: "german"; MessagesFile: "compiler:Languages\German.isl" [Tasks] Name: "desktopicon"; Description: "Desktop-Verknuepfung erstellen"; GroupDescription: "Zusaetzliche Optionen:" -Name: "autostart"; Description: "Bei Windows-Anmeldung automatisch starten"; GroupDescription: "Zusaetzliche Optionen:"; Flags: checkedonce +Name: "autostart"; Description: "Bei Windows-Anmeldung automatisch starten (fuer ALLE Benutzer; einzelne Benutzer spaeter in den Einstellungen)"; GroupDescription: "Zusaetzliche Optionen:"; Flags: checkedonce [Files] ; Hauptanwendung - Pfad anpassen nach Build diff --git a/src/StarfaceOutlookSync/Services/AutostartManager.cs b/src/StarfaceOutlookSync/Services/AutostartManager.cs new file mode 100644 index 00000000..4a6d9d71 --- /dev/null +++ b/src/StarfaceOutlookSync/Services/AutostartManager.cs @@ -0,0 +1,76 @@ +using System; +using System.Diagnostics; +using System.Security.Principal; +using Microsoft.Win32; + +namespace StarfaceOutlookSync.Services +{ + /// + /// Verwaltet den Windows-Autostart ueber die Run-Schluessel: + /// - pro Benutzer -> HKCU\...\Run + /// - alle Benutzer -> HKLM\...\Run (nur mit Admin-Rechten aenderbar) + /// Das Setup setzt den Autostart fuer alle Benutzer (HKLM); hierueber laesst + /// er sich nachtraeglich umstellen oder entfernen. + /// + public static class AutostartManager + { + private const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run"; + private const string ValueName = "StarfaceOutlookSync"; + + private static string ExePath() + { + try + { + var p = Process.GetCurrentProcess().MainModule?.FileName; + if (!string.IsNullOrEmpty(p)) return p; + } + catch { } + return System.Reflection.Assembly.GetEntryAssembly()?.Location ?? ""; + } + + public static bool IsAdmin() + { + try + { + using (var id = WindowsIdentity.GetCurrent()) + return new WindowsPrincipal(id).IsInRole(WindowsBuiltInRole.Administrator); + } + catch { return false; } + } + + public static bool GetUserAutostart() => HasValue(Registry.CurrentUser); + public static bool GetMachineAutostart() => HasValue(Registry.LocalMachine); + + public static void SetUserAutostart(bool enabled) => SetValue(Registry.CurrentUser, enabled); + + /// Setzt/entfernt den Autostart fuer alle Benutzer. Braucht Admin-Rechte. Gibt Erfolg zurueck. + public static bool SetMachineAutostart(bool enabled) => SetValue(Registry.LocalMachine, enabled); + + private static bool HasValue(RegistryKey root) + { + try + { + using (var k = root.OpenSubKey(RunKey, false)) + return k?.GetValue(ValueName) != null; + } + catch { return false; } + } + + private static bool SetValue(RegistryKey root, bool enabled) + { + try + { + using (var k = root.CreateSubKey(RunKey)) + { + if (k == null) return false; + if (enabled) + k.SetValue(ValueName, "\"" + ExePath() + "\"", RegistryValueKind.String); + else if (k.GetValue(ValueName) != null) + k.DeleteValue(ValueName, false); + } + return true; + } + catch { return false; } + } + } +} diff --git a/src/StarfaceOutlookSync/UI/SettingsForm.cs b/src/StarfaceOutlookSync/UI/SettingsForm.cs index ca1afd0f..64aead58 100644 --- a/src/StarfaceOutlookSync/UI/SettingsForm.cs +++ b/src/StarfaceOutlookSync/UI/SettingsForm.cs @@ -9,6 +9,7 @@ namespace StarfaceOutlookSync.UI { private CheckBox _chkStartMinimized, _chkSyncOnStart, _chkAutoAcceptOutlook; private CheckBox _chkNotifGeneral, _chkNotifWarn; + private CheckBox _chkAutostartUser, _chkAutostartAll; private NumericUpDown _numLogRetention; private TextBox _txtSharedDir; private Button _btnBrowseShared; @@ -116,23 +117,48 @@ namespace StarfaceOutlookSync.UI ForeColor = Color.Gray, Font = new Font("Segoe UI", 8) }; + // === Autostart === + bool isAdmin = AutostartManager.IsAdmin(); + + var lblAutostart = new Label + { + Text = "Autostart (bei Windows-Anmeldung starten)", + Left = 12, Top = 332, AutoSize = true, Font = new Font("Segoe UI", 10, FontStyle.Bold) + }; + + _chkAutostartUser = new CheckBox + { + Text = "Nur fuer diesen Benutzer", + Left = 20, Top = 360, AutoSize = true, + Checked = AutostartManager.GetUserAutostart() + }; + + _chkAutostartAll = new CheckBox + { + Text = "Fuer alle Benutzer" + (isAdmin ? "" : " (nur mit Admin-Rechten aenderbar)"), + Left = 20, Top = 386, AutoSize = true, + Checked = AutostartManager.GetMachineAutostart(), + Enabled = isAdmin + }; + _btnSave = new Button { - Text = "Speichern", Left = 95, Top = 334, Width = 85, Height = 28, + Text = "Speichern", Left = 95, Top = 422, Width = 85, Height = 28, DialogResult = DialogResult.None }; _btnSave.Click += (s, e) => Save(); _btnCancel = new Button { - Text = "Abbrechen", Left = 189, Top = 334, Width = 85, Height = 28, + Text = "Abbrechen", Left = 189, Top = 422, Width = 85, Height = 28, DialogResult = DialogResult.Cancel }; - Size = new Size(380, 424); + Size = new Size(380, 512); Controls.AddRange(new Control[] { _chkStartMinimized, _chkSyncOnStart, _chkAutoAcceptOutlook, lblHint, _chkNotifGeneral, _chkNotifWarn, lblLogRetention, _numLogRetention, - lblShared, _txtSharedDir, _btnBrowseShared, lblSharedHint, _btnSave, _btnCancel }); + lblShared, _txtSharedDir, _btnBrowseShared, lblSharedHint, + lblAutostart, _chkAutostartUser, _chkAutostartAll, _btnSave, _btnCancel }); AcceptButton = _btnSave; CancelButton = _btnCancel; } @@ -165,6 +191,16 @@ namespace StarfaceOutlookSync.UI // Sofort anwenden, damit der Effekt direkt sichtbar ist. Logger.PruneOlderThan(_settings.LogRetentionDays); + // Autostart (Registry). Pro Benutzer immer; alle Benutzer nur mit Admin. + AutostartManager.SetUserAutostart(_chkAutostartUser.Checked); + if (AutostartManager.IsAdmin()) + { + if (!AutostartManager.SetMachineAutostart(_chkAutostartAll.Checked)) + MessageBox.Show(this, + "Autostart fuer alle Benutzer konnte nicht geaendert werden (Rechte?).", + "Autostart", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + DialogResult = DialogResult.OK; Close(); }