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) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Security.Principal;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace StarfaceOutlookSync.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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);
|
||||
|
||||
/// <summary>Setzt/entfernt den Autostart fuer alle Benutzer. Braucht Admin-Rechte. Gibt Erfolg zurueck.</summary>
|
||||
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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user