mirror of
https://github.com/ckaczor/ChrisKaczor.Wpf.Application.StartWithWindows.git
synced 2026-01-13 17:22:24 -05:00
Initial commit
This commit is contained in:
38
ApplicationExtensions.cs
Normal file
38
ApplicationExtensions.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace ChrisKaczor.Wpf.Application
|
||||
{
|
||||
public static class ApplicationExtensions
|
||||
{
|
||||
public static void SetStartWithWindows(this System.Windows.Application application, bool value)
|
||||
{
|
||||
var applicationName = Assembly.GetEntryAssembly()!.GetName().Name;
|
||||
|
||||
SetStartWithWindows(application, applicationName!, value);
|
||||
}
|
||||
|
||||
public static void SetStartWithWindows(this System.Windows.Application application, string applicationName, bool value)
|
||||
{
|
||||
var applicationPath = $"\"{Assembly.GetEntryAssembly()!.Location}\"";
|
||||
|
||||
SetStartWithWindows(application, applicationName, applicationPath, value);
|
||||
}
|
||||
|
||||
public static void SetStartWithWindows(this System.Windows.Application _, string applicationName, string applicationPath, bool value)
|
||||
{
|
||||
// Open the run registry key
|
||||
var registryKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true) ?? throw new ApplicationException("Unable to open registry key");
|
||||
|
||||
// Delete any existing key
|
||||
registryKey.DeleteValue(applicationName, false);
|
||||
|
||||
// If auto start should not be on then we're done
|
||||
if (!value) return;
|
||||
|
||||
// Set the registry key
|
||||
registryKey.SetValue(applicationName, applicationPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user