C#Implementing Program Start-Up and Start-Up

Keywords: C# Windows

Recently, I was making a widget that backs up files automatically. I need to use boot-up and self-start

Here's the code

        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
           try
            {
                //Set boot-up self-start  
                if (checkBox8.Checked == true)
                {
                /*Method One*/
                string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonStartup);
                //Get the current path of the file
                string dir = Directory.GetCurrentDirectory();
                //Get all paths to executable files
                string exeDir = dir + @"\Automatic backup.exe.lnk";
                File.Copy(exeDir, StartupPath + @"\Automatic backup.exe.lnk", true);
                /*Method 2*/
                string path = Application.ExecutablePath;
                RegistryKey rk = Registry.LocalMachine;
                RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
                rk2.SetValue("JcShutdown", path);
                rk2.Close();
                rk.Close();

                }
                //Cancel boot-up self-start  
                else
                {
                    /*Method One*/
                    string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonStartup);
                    System.IO.File.Delete(StartupPath + @"\EcgNetPlug.exe.lnk");
                    /*Method 2*/
                    string path = Application.ExecutablePath;
                    RegistryKey rk = Registry.LocalMachine;
                    RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
                    rk2.DeleteValue("JcShutdown", false);
                    rk2.Close();
                    rk.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

The first method works by copying a shortcut to an executable file directly into the system's boot directory, which is not blocked by security software and does not require additional privileges

The second way is to write the registry directly, which may intercept security software

You can try it yourself, you can leave a message if you have problems, I also do it while learning

 

Author: Dream by Dream
Origin: http://www.cnblogs.com/huanjun/
Copyright of this article belongs to both the author and the blog park. Reproduction is welcomed, but this statement must be retained without the author's consent, and the original text link must be provided in an obvious place on the article page, otherwise the right to pursue legal responsibility is reserved.

Posted by cocell on Sat, 04 Apr 2020 16:54:29 -0700