The principle of this method is to check whether the program is running in a specific directory before running the program. If not, copy the program to a specific directory and run the new program, then exit the old program.
Open Delphi, create a project, and write the code in the create event of the window:
Procedure tform1.formcreate (Sender: tobject );
VaR myname: string;
Begin
Myname: = extractfilename (application. exename); // get the file name
If application. exename <> getwindir + myname then // if the file is not in Windowssystem, then ..
Begin
Copyfile (pchar (application. exename), pchar (getwindir + myname), false);... {copy yourself to Windowssystem}
Winexec (pchar (getwindir + myname), sw_hide); // run the new file under Windowssystem
Application. Terminate; // exit
End;
End;
Getwindir is a user-defined function. The function is to find the path of Windowssystem.
Function getwindir: string;
VaR
Buf: array [0 .. max_path] of char;
Begin
Getsystemdirectory (BUF, max_path );
Result: = Buf;
If result [length (result)] <> ''then result: = Result + '';
End;
How can I enable the program to automatically start when Windows is started?
To enable the program to run automatically when Windows is started, there are six ways to achieve this. "glacier" uses the registry.
Add the Registry Unit and rewrite the create event in the preceding window. The rewritten program is as follows:
Procedure tform1.formcreate (Sender: tobject );
Const K = 'softwaremicrosoftwindowscurrentversionrunservice ';
VaR myname: string;
Begin
... {Write by lovejingtao, http://Lovejingtao.126.com, Lovejingtao@21cn.com}
Myname: = extractfilename (application. exename); // get the file name
If application. exename <> getwindir + myname then // if the file is not in Windowssystem, then ..
Begin
Copyfile (pchar (application. exename), pchar (getwindir + myname), false);... {// copy yourself to Windowssystem}
Winexec (pchar (getwindir + myname), sw_hide); // run the new file under Windowssystem
Application. Terminate; // exit
End;
With Tregistry. Create do
Try
Rootkey: = HKEY_LOCAL_MACHINE;
Openkey (K, true );
Writestring ('syspler', application. exename );
Finally
Free;
End;
End;