C # intermediate-Automatic startup program,
I. Preface
For the method of C # automatically starting a program after it is started up, the following statements appear on the Internet: Modify the registry:
1. HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows \ CurrentVersion \ Run or RunOnce
2. HKEY_Current_User \ Software \ Microsoft \ Windows \ CurrentVersion \ Run or RunOnce
Write the name and path of the program you want to run automatically at startup to the Registry,However! This method is problematic:
1. Since Microsoft Vista, the Registry's permission management is more stringent and requires a series of permission operations. To perform registry operations, the app. manifest file is required:
<RequestedExecutionLevel level = "highestAvailable" uiAccess = "false"/>
And you also need
Registry. LocalMachine. OpenSubKey (Yoursubkey,
RegistryKeyPermissionCheck. ReadWriteSubTree, System. Security. AccessControl. RegistryRights. FullControl );
2. I first used the Registry Modification Method When writing the automatic Startup Program for boot, but I found that different computers do not succeed every time, that is to say, the success rate of modifying the registry is not 100%.
2. An old and reliable method
In fact, there is a very reliable and practical method, that is, in the computer [start/start] folder, you want to automatically start the program into the shortcut. In this way, the program in this folder will be executed next time,
Don't forget!If you do not need to execute this program every time you start the system, you should have a statement to delete this shortcut in the program.
The following are components of my automatic Startup Program:
1. The program Restart.exe that needs to be automatically executed at startup
2. a script for executing restart.exe Restart. vbs
If restart.exe and. vbs are in any location on any hard disk of the client, my process is:
1. Generate Restart. vbs shortcut Restart. lnk --> cut. lnk to the Start/start folder.
2. When the next server is started, the local file .lnk .vbs will be used first, and then the file. vbs .exe will be used again.
3. After executing the program in exe, use File. Delete to Delete. lnk (I don't want to execute this. lnk every time I start the system)
Iii. Code Implementation
1. vbs script code (this script is in the same folder as .exe)
Set shell =Wscript.createobject("WScript.Shell") shell.Run "Restart.exe",0,False
2. Create a shortcut code (you need to introduce the COM component named Windows Script Host Object Model)
if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\Restart.lnk")) { IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(System.IO.Path.Combine("Your exe file path", "Restart.lnk")); shortcut.TargetPath = System.IO.Path.Combine("Your exe file path", "Restart.vbs"); shortcut.WorkingDirectory = "Your exe file path"; shortcut.WindowStyle = 7; shortcut.Save(); File.Move(System.IO.Path.Combine("Your exe file path", "Restart.lnk"), Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\Restart.lnk"); }
Note! Last sentence!
File. Move Environment. GetFolderPath (Environment. SpecialFolder. Startup) + "\ Restart. lnk"
Cannot be written
Path. Combine (Environment. GetFolderPath (Environment. SpecialFolder. Startup), "Restart. lnk ")
3. After executing the main code in the exe, delete the shortcut to prevent the program from being executed every time it is started (based on your own needs)
File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\Restart.lnk");