C#  开发WINFORM 程序 使开机时自动运行

解决方案 »

  1.   

      
      /// <summary>
        /// 注册表操作
        /// </summary>
        public class WinRegistryKey
        {
            //自动运行,keyname 是你自定义的注册表键,可以自己取个名字,filepath是要自动运行的程序路径
            public static bool SetAutoRun(string keyName, string filePath)
            {
                try
                {
                    RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                    runKey.SetValue(keyName, filePath);
                    runKey.Close();
                }
                catch
                {
                    return false;
                }
                return true;
            }//取消自动运行
            public static bool UnSetAutoRun(string keyName)
            {
                try
                {
                    RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);                if (runKey.GetValue(keyName) != null)
                    {
                        runKey.DeleteValue(keyName);
                    }
                    runKey.Close();
                }
                catch
                {
                    return false;
                }
                return true;           
            }    }