Preface
Recently, I was studying the program started with the system and found that due to permission issues on win7, there are always problems when writing the registry. If I can't write it in, I cannot start automatically. Then I decided to take a closer look at this problem.
During the process of searching the data, we found that there are three ways to add to the startup, namely:
- Start menu starts;
- Registry start key;
- Windows schedule tasks.
1. Start menu
A long time ago, when I first started to do a computer, I knew a way to start the computer. At that time, I directly created and copied the shortcuts of the program to the "Start" menu. It was tried and tested. Of course, at that time, I created them manually and dragged them to the startup. Implementing with programs is not complicated.
There are two steps in total:
- Find the directory associated with Start menu in the Start menu;
- Create a shortcut to this directory;
Create a shortcut code:
public static bool Create(string directory, string shortcutName, string targetPath, string description = null, string iconLocation = null) { try { if (!(directory)) { (directory); } //Add a reference Search in Com Windows Script Host Object Model string shortcutPath = (directory, ("{0}.lnk", shortcutName)); shell = new (); shortcut = ()(shortcutPath);//Create a shortcut object = targetPath;//Specify the target path = (targetPath);//Set the starting position = 1;//Set the operation mode, default to the regular window = description;//Setting Notes = (iconLocation) ? targetPath : iconLocation;//Set icon path ();//Save shortcut return true; } catch { } return false; }
Note: When using WshShell, you need to add a reference. Search from Com: Add Windows Script Host Object Model.
Get the location of the Start Menu Start folder:
// Get global Start folder location(); // Get the start folder location of the currently logged in user();
2. Registry startup items
I believe this is a situation that most students use. It is simple and easy to understand and hidden (it just feels very hidden, but msconfig is immediately exposed). The code is very simple. Just add the started project name and file location to the startup item.
// Add to the registry startup key of the currently logged in userRegistryKey RKey = (@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); ("AppName", @"C:\"); // Add to all users' registry startup keysRegistryKey RKey = (@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); ("AppName", @"C:\");
It should be noted here that when you use regedit to view the registry startup key, you will find that some startup keys are enclosed in double quotes:
AppName C:\
or
AppName “C:\”
If your path has spaces, you need to use double quotes to ensure that there will be no problems when starting, as follows:
// Double quotes in @ symbols need to be escaped twice("AppName", @"""C:\""");
3. Start Windows scheduled task mode
The addition of startup of Windows' task scheduled method can also be achieved by self-starting, but I personally feel that this method is a bit weird...
Code:
// Create a new taskTaskSchedulerClass scheduler = new TaskSchedulerClass(); //connect(null, null, null, null); //Get the directory where the task was createdITaskFolder folder = ("\\"); //Set parametersITaskDefinition task = (0); = author;//Creator = desc;//describe//Set the trigger mechanism (here is after login)(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON); //Set the action (here is the running exe program)IExecAction action = (IExecAction)(_TASK_ACTION_TYPE.TASK_ACTION_EXEC); = file;//Set the file directory = "PT0S"; //Does the task time time expire to stop the task? PTOS does not turn on timeout = false;//Only executed under AC power = false;//Execute only if the computer is idle IRegisteredTask regTask = (name, task,//The name of the task needs to be set here (name) (int)_TASK_CREATION.TASK_CREATE, null, //user null, // password _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); IRunningTask runTask = (null);
Note: The task plan requires adding a reference. Search for TaskScheduler in Com, and add it. Set "Embed Interoperable Type" to false so that when the program is compiled, you can obtain the type information of the COM type from the interoperable assembly.
Summary code
The above are three methods added to system startup, source code:Source code download
This is the article about C# adding three ways to boot the program. For more related C# program startup content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!