CSharp程序设置自动启动

using System;
using System.Text;
using Microsoft.Win32;

namespace GPJJ
{
    class XWin32Utils
    {
        private const String KEY_PATH = "SOFTWARE\\MICROSOFT\\WINDOWS\\CURRENTVERSION\\RUN";
        private const String VALUE_NAME = "GPJJEXE";

        public static bool isAutoRun()
        {
            RegistryKey key = Registry.LocalMachine;
            RegistryKey autorun = key.OpenSubKey(KEY_PATH, true);
            Object o = autorun.GetValue(VALUE_NAME);
            autorun.Close();

            if(o==null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public static void SetAutoRun(String exePath)
        {
            RegistryKey key = Registry.LocalMachine;
            RegistryKey autorun = key.OpenSubKey(KEY_PATH, true);

            autorun.SetValue(VALUE_NAME, exePath, RegistryValueKind.String);
        }

        public static void RemoveAutoRun()
        {
            if(isAutoRun())
            {
                RegistryKey key = Registry.LocalMachine;
                RegistryKey autorun = key.OpenSubKey(KEY_PATH, true);
                autorun.DeleteValue(VALUE_NAME);
            }
        }
    }
}

Comments are closed.