SoFunction
Updated on 2025-03-06

Implementation example of C# setting program startup

1: Get the current user:

  identity = ();
             principal = new (identity);

2: Determine whether the current user is an administrator. If so, start directly. Otherwise, start through Process:

(If you do not handle this, you will report an exception if you directly use non-admin permissions to edit the registry operation program)

if (())
            {
                //If it is an administrator, start it directly                (new Form1());
            }
            else
            {
                 startinfo = new ();
                //Start application                 = ;
                //Set the startup action to start as administrator                 = "runas";
              var process=  (startinfo);
                ();
            }

3: Edit the registry and set the startup path

 RegistryKey runKey = (@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
                //The currently launched project
                //string app = ;
                //The path format obtained is: D:\Program Files (x86)/360/360Safe/safemon/                //This format cannot achieve the purpose of booting up.
                string app = ().Location;
                //Format: D:\Program Files (x86)\360\360Safe\safemon\                //This format realizes booting      
                (@"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN").SetValue("MyAngel", app, ); 		//Open an existing key in the registry and set the key value type in it

4: Log out the power-on self-start function (optional):

 //Delete the startup item            RegistryKey runKey = (@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", );
            
            ("MyAngel");
            ();

5: Special precautions:

1, although using:

(@"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN").SetValue("MyAngel", app, );

Theoretically, the added key value information should be stored in:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

It is actually possible to store in:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run

2. The format of the set value must be paid attention to:

SetValue("MyAngel", app, );

The string format stored by the app can only be:D:\Program Files (x86)\360\360Safe\safemon\

And not:D:\Program Files (x86)/360/360Safe/safemon/

If the format is abnormal, the purpose of self-start is not achieved.

This is the article about the implementation example of C# setting up the startup of the program. For more related contents of C# program startup, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!