📜  启动 c# 类 winform - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:13.785000             🧑  作者: Mango

代码示例1
using System;
using System.Windows.Forms;
using Microsoft.Win32;

public class StartupManager : Form
{
    // The path to the key where Windows looks for startup applications
    static RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    /// 
    /// Checking if the startup is already enabled or not
    /// 
    /// Returns the state of the startup
    public static bool IsStartupInitialized()
    {
        return rkApp.GetValue(Application.ProductName) != null;
    }
    /// 
    /// Changing the app startup state
    /// 
    /// Set startup state to
    public void SetStartupState(bool to)
    {
        if (to)
        {
            // Add the value in the registry so that the application runs at startup
            rkApp.SetValue(Application.ProductName, Application.ExecutablePath);
        }
        else
        {
            // Remove the value from the registry so that the application doesn't start
            rkApp.DeleteValue(Application.ProductName, false);
        }
     }
}