In the preparation of the maintenance phase after the application, we will face the work upgrade to the application. Undoubtedly, the manual upgrade work is very cumbersome, you may need to update the file up to the user's machine. However, if you can edit one Automatic upgrade program, put on the network, let the user download, just implement the upgrade work, isn't it very convenient? In fact, such a program can be easily prepared using Delphi.
The following is our example, set up to have an application this.exe, upgrade it, the program used to upgrade is Upgrade.exe.
1. THIS's project file frame
PROGRAM THIS;
Uses
SYSUTILS,
Windows,
Forms,
...;
We write it into your code because this is not automatically generated in Delphi's engineering file. "..." is the form you created.
VAR
FN: String;
TSI: TSTARTUPINFO;
TPI: TPROCESSINFORMATION;
{$ R * .res}
Begin
"Upgrade Program" uses fileage to check the original application's establishment time, if the old version is detected, the prompt is upgraded:
IF fileage ('SOMSERVERSOMEDIRTHIS.EXE')> Fileage (paramstr (0)) THEN
If Application.MessageBox ('This is a newer version of this.exe.' # 13 'Do you have new version of the upgrade file?', 'Upgrade Time!', 1) = 1 THEN
Begin
When running an upgrade program, the upgrade program should be in the same directory with the original program this.exe. If it starts, this version of the main program should exit, but to ensure that it does not delete any files, because at this time Application.initialize Not called. The code is as follows:
FN: = ExtractFilePath (paramstr (0)) 'UpgradetHis.exe';
Fillchar (TSI, SIZEOF (TSI), 0);
TSI.cb: = SizeOf (TSI);
IF CreateProcess (Pchar (FN), NIL, NIL, NIL, FALSE,
Detached_Process, NIL, NIL, TSI, TPI)
Then EXIT
However, if the "Upgrade Procedure" is not running, we should tell the user at this time, he can get an updated program version through other methods, then we still exit the main program so that the user is upgraded. If necessary, you can also Continue to load and run the old version.
Else
Begin
MessageBeep (0);
Application.MessageBox ('can't complete file copy.' '' Please manually copy! ',' Copy Error! ', 1);
EXIT;
END;
END;
Application.INITIALIZE;
Application.title: = 'THIS';
Application.helpfile: = 'THIS.HLP';
{AutoCreate Forms, etc.}
Application.run;
End.
If the automatic upgrade program is a console program, you must ensure the correct console program connection option settings.
PROGRAM UPGRADETHIS;
Uses
Forms,
SYSUTILS,
WINDOWS;
VAR
FN: String;
TSI: TSTARTUPINFO;
TPI: TPROCESSINFORMATION;
{$ R * .res}
Begin
Use copyfile to copy the new version of the file, like Fileage, it does not consider the path and file name of the standard naming format .IF CopyFile ('SOMESERVERSOMEDIRTHIS.EXE',
Pchar (ExtractFilePath (paramstr (0)) 'this.exe'), False) THEN
Begin
Once these new versions of files are copied into the system, it should be able to run, and there is no reason not to run. So, this is basically no need to join the error detected code.
Then, the code that makes it run and simply handles the error as follows:
FN: = ExtractFilePath (paramstr (0)) 'this.exe';
Fillchar (TSI, SIZEOF (TSI), 0);
TSI.cb: = SizeOf (TSI);
CreateProcess (PCHAR (FN), NIL, NIL, NIL, FALSE, DETACHED-Process, NIL, NIL, TSI, TPI;
End
Else
Application.MessageBox ('can't copy file this.exe', 'error!', MB_OK);
End.
It should be noted that in these code used to upgrade the program, no application.initialize and application.run are not used. These methods can also be referenced to check the new version of the help file (with getWindowsDir or getWindowsDirectory) Help "Add to the program used to upgrade) or to detect if the Upgrade Program is a new version.
The above is just from the update perspective of the file, the method of the "Upgrade Program" is described, but in many cases, upgrade a program even to modify the registry or various INI files, which is easier to implement in Delphi, interested Friends can refer to some information to join the code that implements these features.