AlexSJC
发布于 2023-12-09 / 10 阅读
0
0

C# 开机启动

using Microsoft.Win32;
using System.Diagnostics;

public class AutoStart
{
    public const string KeyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";

    public static void Set(bool value)
    {
        using (var key = Registry.CurrentUser.OpenSubKey(KeyPath, true))
        {
            var process = Process.GetCurrentProcess();
            if (value)
            {
                key.SetValue(process.ProcessName, process.MainModule.FileName);
            }
            else
            {
                key.DeleteValue(process.ProcessName, false);
            }
        }
    }

    public static bool IsEnabled()
    {
        using (var key = Registry.CurrentUser.OpenSubKey(KeyPath))
        {
            var process = Process.GetCurrentProcess();
            return key.GetValue(process.ProcessName) != null;
        }
    }
}


评论