-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb39f9b
commit 85a3cfd
Showing
3 changed files
with
91 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Microsoft.Win32; | ||
using System; | ||
using System.Diagnostics; | ||
using System.Reflection; | ||
|
||
namespace Knatter.Core | ||
{ | ||
public static class AutoRun | ||
{ | ||
private const string RegKey = @"Software\Microsoft\Windows\CurrentVersion\Run"; | ||
private static readonly string RegValue = ProgramInfo.Name; | ||
|
||
public static bool? IsEnabled() | ||
{ | ||
try | ||
{ | ||
return Registry.GetValue($"HKEY_CURRENT_USER\\{RegKey}", RegValue, null) is string; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.WriteLine("Error reading autorun key: "+ex); | ||
return null; | ||
} | ||
} | ||
|
||
public static bool? Enable(bool enabled, string commandLine) | ||
{ | ||
try | ||
{ | ||
bool? isEnabled = IsEnabled(); | ||
if (!isEnabled.HasValue) | ||
return isEnabled; | ||
|
||
if (isEnabled.Value == enabled) | ||
return isEnabled; | ||
|
||
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RegKey, true)) | ||
{ | ||
if (enabled) | ||
{ | ||
Debug.WriteLine($"Adding regkey for autostart: {RegValue}='{commandLine}'"); | ||
key.SetValue(RegValue, commandLine); | ||
} | ||
else | ||
{ | ||
Debug.WriteLine($"Removing regkey for autostart: {RegValue}"); | ||
key.DeleteValue(RegValue); | ||
} | ||
key.Dispose(); | ||
key.Flush(); | ||
} | ||
return enabled; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.WriteLine("Error writing autorun key: " + ex); | ||
return null; | ||
} | ||
} | ||
|
||
} | ||
} |