Skip to content

Commit

Permalink
add functions: App.CheckStartWithOs(), App.SetStartWithOs()
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed May 18, 2024
1 parent e7bb0b2 commit 02f8b39
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
57 changes: 57 additions & 0 deletions Source/Components/ImageGlass.Base/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using Microsoft.Win32;
using System.Diagnostics;
using System.Security.Principal;

Expand Down Expand Up @@ -95,4 +96,60 @@ public static string ConfigDir(PathType type, params string[] paths)
}



/// <summary>
/// Checks if ImageGlass starts with OS
/// </summary>
public static bool CheckStartWithOs()
{
const string APP_NAME = "ImageGlass";
var regAppPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";


try
{
using var key = Registry.CurrentUser.OpenSubKey(regAppPath);
var keyValue = key?.GetValue(APP_NAME)?.ToString();

var isEnabled = !string.IsNullOrWhiteSpace(keyValue);

return isEnabled;
}
catch { }

return false;
}


/// <summary>
/// Sets or unsets ImageGlass to start with OS.
/// Returns <c>true</c> if successful.
/// </summary>
public static bool SetStartWithOs(bool enable)
{
const string APP_NAME = "ImageGlass";
var regAppPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";

try
{
using var key = Registry.CurrentUser.OpenSubKey(regAppPath, true);

if (enable)
{
key?.SetValue(APP_NAME, $"{App.IGExePath} ${IgCommands.PRELOAD_OS}");
}
else
{
key?.DeleteValue(APP_NAME);
}

return true;
}
catch { }


return false;
}


}
2 changes: 1 addition & 1 deletion Source/Components/ImageGlass.Base/BHelper/General.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static bool IsOS(WindowsOS ver)
/// Checks if the OS is Windows 10 or greater or equals the given build number.
/// </summary>
/// <param name="build">Build number of Windows.</param>
public static bool IsOSBuildOrGreater(int build = -1)
public static bool IsOSBuildOrGreater(int build)
{
return Environment.OSVersion.Version.Major >= 10
&& Environment.OSVersion.Version.Build >= build;
Expand Down
1 change: 1 addition & 0 deletions Source/Components/ImageGlass.Base/Types/IgCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static class IgCommands
public static string SHOW_UI => "--ui";
public static string HIDE_ADMIN_REQUIRED_ERROR_UI => "--hide-admin-error-ui";
public static string PER_MACHINE => "--per-machine";
public static string PRELOAD_OS => "--preload-os";


// igcmd.exe
Expand Down

0 comments on commit 02f8b39

Please sign in to comment.