-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: option to remember Enabled state between reruns (#7)
- added option to remember Enabled state between reruns - code refactoring - a small cleanup - bug fixed - Enabling NoSleep didn't immediately apply the state - moved app-wide settings out of the code, to native app.config - removed compiled binary (see releases) and outdated readme.html
- Loading branch information
Showing
11 changed files
with
331 additions
and
305 deletions.
There are no files selected for viewing
Binary file not shown.
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
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
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,66 @@ | ||
using System; | ||
using System.IO; | ||
using System.Windows.Forms; | ||
|
||
namespace NoSleep | ||
{ | ||
public static class Tools | ||
{ | ||
/// <summary> Create a shortcut at given path with given link.</summary> | ||
/// <param name="targetPath"> Where to target the shortcut, i.e. what to run on shortcut usage.</param> | ||
/// <param name="shortcutPath"> Where to create the shortcut and how to name it.</param> | ||
/// <exception cref="Exception"> On IO related issues, including permissions.</exception> | ||
public static void CreateShortcut(string targetPath, string shortcutPath) | ||
{ | ||
var shell = new IWshRuntimeLibrary.WshShell(); | ||
var shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); | ||
shortcut.TargetPath = targetPath; | ||
shortcut.Save(); | ||
} | ||
/// <summary> Path to the autostart shortcut. </summary> | ||
private static readonly string autostartPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), $"{Properties.Settings.Default.AppStartupName}.lnk"); | ||
|
||
/// <summary> Check application startup state </summary> | ||
/// <returns><see langword="true"/> if application startup is enabled.</returns> | ||
internal static bool AutostartCheck() | ||
=> File.Exists(autostartPath); | ||
|
||
/// <summary> Disable application startup on user login. </summary> | ||
/// <returns><see langword="true"/> if shortcut is no longer present.</returns> | ||
internal static bool AutostartDisable() | ||
{ | ||
// If autostart shortcut exists - try to remove it | ||
if (File.Exists(autostartPath)) | ||
{ | ||
try | ||
{ | ||
File.Delete(autostartPath); | ||
} | ||
catch (Exception e) | ||
{ | ||
MessageBox.Show($"Wasn't able to remove autostart shortcut from '{autostartPath}'. Error: {e.Message}", | ||
caption: Properties.Settings.Default.AppName, buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/// <summary> Enable application startup on user login </summary> | ||
/// <returns><see langword="true"/> if shortcut was created.</returns> | ||
internal static bool AutostartEnable() | ||
{ | ||
try | ||
{ | ||
CreateShortcut(Application.ExecutablePath, autostartPath); | ||
} | ||
catch (Exception e) | ||
{ | ||
MessageBox.Show($"Wasn't able to create autostart shortcut at '{autostartPath}'. Error: {e.Message}", | ||
caption: Properties.Settings.Default.AppName, buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
} |
Oops, something went wrong.