C # Execute BAT file batch-enable application startup function

Source: Internet
Author: User

When you recently worked on a project (platform. NET 4.0 WinForm), the customer asked the software to provide boot-start Setup options

At the beginning, the implementation method is as follows:

 Public classBoot {//writing to the registry         Public Static voidBootfromboot (stringEXEName,stringExePath) {RegistryKey Rkey=Registry.localmachine; RegistryKey AutoRun= Rkey.createsubkey (@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); Try{autorun.setvalue (exename, ExePath); }            Catch(System.Exception ex) {Throwex; }        }        //Delete Registry         Public Static voidDeletefromboot (stringEXEName,stringExePath) {RegistryKey Rkey=Registry.localmachine; RegistryKey AutoRun= Rkey.createsubkey (@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); Try            {                //Autorun.deletevalue (exepath);                if(Autorun.getvalue (exename)! =NULL) Autorun.deletevalue (exename); }            Catch(System.Exception ex) {Throwex; }        }

However, there is a problem, when the application does not have administrator rights, write to the registry will fail, in the WinForm project properties open to get administrator rights

This time, you can not drag the desktop shortcut into the application when the application runs on the WIN10 system, error,

The reason is: To cancel the administrator permissions, the win10 on the normal operation, but the boot start to write to the registry is not,

The head is still working (thanks to the wisdom of God), the thought of registering a registry write with a batch file of Windows

#regionBAT Batch Processing Public Static voidCreateregdeletebat (stringFile_path,stringkey) {            if(System.IO.File.Exists (File_path)) {System.IO.File.Delete (file_path); }            stringBAT ="@echo off"+"\ r \ n"; Bat+="cacls.exe \ "%systemdrive%\\system Volume information\" >nul 2>nul"+"\ r \ n"; Bat+="if%errorlevel%==0 goto Admin"+"\ r \ n"; Bat+="Echo Request Administrator"+"\ r \ n"; Bat+="if exist \ "%temp%\\getadmin.vbs\" del/f/q \ "%temp%\\getadmin.vbs\""+"\ r \ n"; Bat+="echo Set requestuac = createobject^ (\ "shell.application\" ^) >\ "%temp%\\getadmin.vbs\""+"\ r \ n"; Bat+="echo requestuac.shellexecute \ "%~s0\", \ "\", \ "\", \ "Runas\", 1 >>\ "%temp%\\getadmin.vbs\""+"\ r \ n"; Bat+="echo wscript.quit >>\ "%temp%\\getadmin.vbs\""+"\ r \ n"; Bat+="\ "%temp%\\getadmin.vbs\"/F"+"\ r \ n"; Bat+="if exist \ "%temp%\\getadmin.vbs\" del/f/q \ "%temp%\\getadmin.vbs\""+"\ r \ n"; Bat+="Exit"+"\ r \ n"; Bat+=": Admin"+"\ r \ n"; Bat+="echo Delete Reg"+"\ r \ n"; Bat+="reg delete \ "hkey_local_machine\\software\\microsoft\\windows\\currentversion\\run\"/V"+ key +"/ F"+"\ r \ n"; //bat + = "PAUSE";System.IO.File.WriteAllText (File_path, Bat, Encoding.default); }         Public Static voidCreateregaddbat (stringFile_path,stringKeystringExefullpath) {            if(System.IO.File.Exists (File_path)) {System.IO.File.Delete (file_path); }            stringBAT ="@echo off"+"\ r \ n"; Bat+="cacls.exe \ "%systemdrive%\\system Volume information\" >nul 2>nul"+"\ r \ n"; Bat+="if%errorlevel%==0 goto Admin"+"\ r \ n"; Bat+="Echo Request Administrator"+"\ r \ n"; Bat+="if exist \ "%temp%\\getadmin.vbs\" del/f/q \ "%temp%\\getadmin.vbs\""+"\ r \ n"; Bat+="echo Set requestuac = createobject^ (\ "shell.application\" ^) >\ "%temp%\\getadmin.vbs\""+"\ r \ n"; Bat+="echo requestuac.shellexecute \ "%~s0\", \ "\", \ "\", \ "Runas\", 1 >>\ "%temp%\\getadmin.vbs\""+"\ r \ n"; Bat+="echo wscript.quit >>\ "%temp%\\getadmin.vbs\""+"\ r \ n"; Bat+="\ "%temp%\\getadmin.vbs\"/F"+"\ r \ n"; Bat+="if exist \ "%temp%\\getadmin.vbs\" del/f/q \ "%temp%\\getadmin.vbs\""+"\ r \ n"; Bat+="Exit"+"\ r \ n"; Bat+=": Admin"+"\ r \ n"; Bat+="echo Add reg"+"\ r \ n"; Bat+="reg add \ "hkey_local_machine\\software\\microsoft\\windows\\currentversion\\run\"/V"+ key +"/ D"+"\""+ Exefullpath +"\""+"/ F"+"\ r \ n"; //bat + = "PAUSE";System.IO.File.WriteAllText (File_path, Bat, Encoding.default); }         Public Static voidRunbatfile (stringFile_path,stringfile_name) {Process Pro=NewProcess (); Pro. Startinfo.filename=file_name; Pro. Startinfo.workingdirectory=File_path; Pro. Startinfo.createnowindow=true; Pro.            Start (); Pro.        WaitForExit (); }        #endregion

This is used when:

Private voidReg_add () {stringExe_path =Application.startuppath; stringfile_name ="Add_reg.bat"; stringFull_path = Exe_path +"\\"+file_name; stringIconsyncpath =Application.executablepath; Filehelper.createregaddbat (Full_path,"Iconsync", Iconsyncpath);        Filehelper.runbatfile (Exe_path, file_name); }        Private voidReg_delete () {stringExe_path =Application.startuppath; stringfile_name ="Delete_reg.bat"; stringFull_path = Exe_path +"\\"+file_name; Filehelper.createregdeletebat (Full_path,"Iconsync");        Filehelper.runbatfile (Exe_path, file_name); }

C # Execute BAT file batch-enable application startup function

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.