通过添加删除注册表键值控制应用程序的自启动

来源:互联网 发布:slam算法简介 编辑:程序博客网 时间:2024/06/05 17:43
                private 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;
                }


                private static bool DeleteAutoRun(string keyName)
                {
                        try
                        {
                                RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                                runKey.DeleteValue(keyName);
                                runKey.Close();
                        }
                        catch
                        {
                                return false;
                        }
                        return true;

                }


调用:

IsAutoRun = AppConfig.GetAutoRun();
                        if (IsAutoRun)
                        {
                                SetAutoRun("应用程序名称", Application.ExecutablePath);
                        }
                        else
                        {
                                DeleteAutoRun("应用程序名称");
                        }

原创粉丝点击