C # Intermediate-Boot Autostart Program

Source: Internet
Author: User

First, preface

About the method of the C # power-on auto-start program, the net appears more is to 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 start automatically, and the registry will be able to boot automatically, but ! There is a problem with this approach:

1. Microsoft has more stringent permissions on the registry since Vista, requiring a series of permissions operations that require app.manifest files for registry operations:

<requestedexecutionlevel level= "Highestavailable" uiaccess= "false"/>

And when you operate the registry, you also need to

Registry.LocalMachine.OpenSubKey (Yoursubkey,

Registrykeypermissioncheck.readwritesubtree,system.security.accesscontrol.registryrights.fullcontrol); Achieve Full Control

2. I also use the method of modifying the registry when I write the autostart program, but I find that it is not always successful for different computers, that is to say, the success rate of the method of modifying the registry is not 100%.

Second an ancient and reliable method

In fact, there is a very reliable and practical way, that is, in the computer "Start/start" folder, you want to boot automatically start the program's shortcut into the. So the next time the boot will execute this folder program,

Don't forget ! If you do not have to execute this program every time you boot, you should have a statement that deletes the shortcut in the program.

Here are the components of my boot Automation program:

1. The program that needs to be powered on automatically Restart.exe

2. A script that directs the execution of Restart.exe Restart.vbs

If Restart.exe and. vbs are anywhere on any of the client's hard disks, my process is:

1. Generate Restart.vbs shortcuts restart.lnk--> cut. lnk into the Start/Start folder

2. The next time you turn on the. LNK executes the. vbs, and then executes the. vbs. EXE

3. When the EXE program is finished, use File.delete to delete the. LNK (I don't want to do this. lnk every time you boot)

Third, the code implementation

1. vbs script code (this script must be in the same folder as the. exe)

Set Shell =wscript.createobject ("wscript.shell")  "  Restart.exe",0, False

2. Create the code for the shortcut (you need to introduce a COM component with the name Windows Script Host Object Model)

 if(! File.exists (Environment.getfolderpath (Environment.SpecialFolder.Startup) +"\\Restart.lnk") {Iwshruntimelibrary.wshshell Shell=NewIwshruntimelibrary.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"); }

Watch out! One 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 each boot from executing the program (depending on your needs)

" \\Restart.lnk ");

C # Intermediate-Boot Autostart Program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.