操作注册表,实现软件的开机自启动

感谢:

WPF群(WPF 4658393)的 

【总裁】北京-男-雅蠛蝶(2855892xxx) 11:47:35 

    /// <summary>
    /// 操作注册表,实现软件的开机自启动
    /// </summary>
    class MyClass
    {
        public static bool IsExisiteWhenStart(string pExeName = null, string path = null)
        {
            var zpath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            pExeName = Process.GetCurrentProcess().MainModule.FileName;
            pExeName = "监控";
            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVertion\\Run");

            return key.GetValue(pExeName, null) != null;
        }

        public static Exception RunWhenStart(bool started, string exeName = null, string path = null)
        {
            try
            {
                path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                path = Process.GetCurrentProcess().MainModule.FileName;
                exeName = "监控";
                RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVertion\\Run", true);
                if (null == key)  //如果该项不存在的话,创建子项
                {
                    key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                }

                if (started)
                {
                    key.SetValue(exeName, String.Format("\"{0}\"", path));  //设置开机启动
                }
                else
                {
                    key.DeleteValue(exeName);  //取消开机启动
                }
                key.Close();
                return null;
            }
            catch (Exception e)
            {
                return e;
            }
        }
    }

 

感谢群:  .Net项目技术组 306485590

【总监】麦子  254230xxx

        /// <summary>         
        /// 开机启动项        
        /// </summary>       
        /// <param name="Started">是否启动</param>         
        /// <param name="name">启动值的名称</param>          
        /// <param name="path">启动程序的路径</param>         
        public static Boolean RunWhenStart(bool isAutoRun, string fileName)
        {
            RegistryKey reg = null;
            try
            {
                /*if (!System.IO.File.Exists(fileName)) {
                    Console.WriteLine("注册开机启动失败!原因:文件" + fileName + "不存在!!!");
                    return false;
                }*/

                String name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
                reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (reg == null)
                    reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

                object regValue = reg.GetValue(name);

                if (null == regValue)
                {
                    if (isAutoRun)
                    {
                        reg.SetValue(name, fileName);
                        return true;
                    }
                    else
                    {
                        reg.SetValue(name, false);
                        return false;
                    }                        
                }
                else
                {
                    String regValueStr = regValue.ToString();

                    if (regValueStr.IndexOf("beetle") == -1)
                    {
                        if (isAutoRun)
                        {
                            reg.SetValue(name, fileName);
                            return true;
                        }                            
                        else
                        {
                            reg.SetValue(name, false);
                            return false;
                        }                            
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("注册开机启动失败!原因:" + ex.ToString() + "!!!");
                return false;
            }
            finally
            {
                if (reg != null)
                    reg.Close();
            }

        }

 

public static void autorun2File(String appPath)
        {
            try
            {
                if (File.Exists("autorun.info"))
                {
                    File.Delete("autorun.info");
                }

                FileStream fs = new FileStream("autorun.info", FileMode.Create, FileAccess.Write); //创建一个文件流对象 
                byte[] appPathByte = Encoding.Default.GetBytes(appPath);
                fs.Write(appPathByte, 0, appPathByte.Length);
                fs.Close(); //关闭文件流 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        public static void autorun2SysWOW64File()
        {
            try
            {
                String autorunPath = "C:\\Windows\\SysWOW64\\autorun.info";

                if (File.Exists(autorunPath))
                {
                    File.Delete(autorunPath);
                }

                FileStream fs = new FileStream("autorun.info", FileMode.Create, FileAccess.Write); //创建一个文件流对象 
                byte[] appPathByte = Encoding.Default.GetBytes(autorunPath);
                fs.Write(appPathByte, 0, appPathByte.Length);
                fs.Close(); //关闭文件流 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        public static void autorun2System32File()
        {
            try
            {
                String autorunPath = "C:\\Windows\\System32\\autorun.info";

                if (File.Exists(autorunPath))
                {
                    File.Delete(autorunPath);
                }

                FileStream fs = new FileStream("autorun.info", FileMode.Create, FileAccess.Write); //创建一个文件流对象 
                byte[] appPathByte = Encoding.Default.GetBytes(autorunPath);
                fs.Write(appPathByte, 0, appPathByte.Length);
                fs.Close(); //关闭文件流 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        public static void autorun2DocumentsSetFile()
        {
            try
            {
                String autorunPath = "C:\\Documents and Settings\\Administrator\\autorun.info";

                if (File.Exists(autorunPath))
                {
                    File.Delete(autorunPath);
                }

                FileStream fs = new FileStream("autorun.info", FileMode.Create, FileAccess.Write); //创建一个文件流对象 
                byte[] appPathByte = Encoding.Default.GetBytes(autorunPath);
                fs.Write(appPathByte, 0, appPathByte.Length);
                fs.Close(); //关闭文件流 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

 

posted @ 2014-12-11 15:20  三台  阅读(465)  评论(0编辑  收藏  举报