c/S program automatic upgrade is a very important function, the principle is actually very simple, generally contains two programs one is the main program, that is, in addition to the upgrade function of the program, the other is the upgrade program, Common 360, Jinshan security guards are like this.
mainly includes the following points: 1 compare version 2 download file 3 update file 4 start the main program. But there are a lot of details that need attention.
The general server will have a configuration file that contains the most recent updated file information, and of course these updates can be stored in the database, or elsewhere. The client (that is, the part of the program that needs to be updated) also has a configuration file containing the client version information, which can be stored in a specific configuration file, or a config file, there is no certain rules, according to the actual design.
When the client program starts, start the update program by comparing the local version and the latest version information on the server to determine if there is a new version, if there is a direct download, the download completes the replacement successful and update the client version information, start the main program.
Disadvantage: If the update speed due to the updated file is large or slow, users have to wait a long time until the download is complete or download failed.
Advantages: After the processing is completed, the startup is directly the updated program. Errors that do not occur because the main program prompts the file when it is running causing the replacement file to be used, cannot be replaced.
Alternatively, when the client segment program starts, the update is started, but the updater does not make the version judgment, check the client update directory to see if there is a new version of the download, if any, update the main program and update the client version information, and then start the main program, if not, start the main program directly. The main program determines whether there is a new version, and in the background download the file into the client update directory, after the download is complete, prompts the user to exit the main program, restart, at startup by the update program and update client and client version information.
Disadvantage: Because the download is running in the background of the main program, it may affect the processing speed of the main program.
Pros: Avoid long waits for users due to download.
1 Compare versions
Comparison basis:
You can use the last modified time of the file, or use the file version as the basis for comparison, using the file last modified time is obviously not the standard practice, but there is no error, but note that the format of the date must be uniform, to avoid inconsistent date format resulting in errors. You can use the FileInfo class to get the last modification time, note that time should take the server time, and compile the assembly machine time should be the same, otherwise it may cause confusion.
FileInfo Class official Website reference
Use the file version as standard, then each modification must modify the version number, C # program is to repair the contents of the AssemblyInfo.cs file, a step more, more specifications. The version class handles versioning information and compares it.
[CSharp]View Plaincopy
- Assembly Thisassem = assembly.getexecutingassembly ();
- AssemblyName thisassemname = Thisassem.getname ();
- Version ver = thisassemname.version;
Version Type official website reference
Of course there are other ways, such as MD5 checksum comparison, file size comparison, and the like. But personally think the file size defect is obvious, the new version of the file must be larger than the old file? Not necessarily. Refactoring is likely to become smaller.
Of course, if you consider a different version of the client, you need to upgrade to the latest version, obviously different versions of the corresponding upgrade files are different, will be more complex, more information to compare.
To obtain service-side version information:
If the server version information exists in the database, directly read the database, it can be obtained. If the configuration file exists, it can be obtained through the WebService method, or request a Web page through Response.Write (), the way to obtain information, of course, both of these methods to establish a virtual directory or Web site.
2 Downloading files
Storage location:
If the new version of the file exists in the database, read the database directly, but this way is not recommended for individuals, such as a large update file performance is not very good.
The existence of a fixed virtual directory under the specified path is a good way, but the client to download, so be careful to assign download permissions.
Download mode:
directly to the request through the virtual path, download the file, because the virtual path has download permissions, if the update needs to determine whether there is permission, such as to pay after the download is not good processing.
Another way is to send a request to a Web page, pass a different query string, the Web page through Response.BinaryWrite (), the way to download the file, you can judge the permissions, of course, some trouble is to avoid.
Download the file code
[CSharp]View Plaincopy
- Uri uri = new Uri (Downfileurl + localfilename);
- HttpWebRequest request = (HttpWebRequest) webrequest.create (URI);
- Request. Credentials = CredentialCache.DefaultCredentials;
- Request. Maximumautomaticredirections = 4;
- LocalFilename = Path.getfilename (LocalFilename);
- using (HttpWebResponse response = (HttpWebResponse) request. GetResponse ())
- {
- Stream Receivestream = Response. GetResponseStream ();
- string NewPath = Path.Combine (Tempfold, LocalFilename);
- using (FileStream fs = new FileStream (NewPath, FileMode.Create))
- {
- byte[] buffer = new byte[4096];
- int bytesread = receivestream.read (buffer, 0, buffer. Length);
- While (Bytesread > 0) {
- Fs. Write (buffer, 0, bytesread);
- Bytesread = receivestream.read (buffer, 0, buffer. Length);
- }
- }
- Receivestream.close ();
- }
3 Update files
Update Type:
A direct replacement, such as a bug modified, is replaced directly.
New additions, such as new additions, have been made into the new class library.
Something that needs to be removed, such as some features that are not needed because of refactoring or using new methods.
To perform, such as writing the registry, registering the COM component.
Each treatment is different and needs to be handled separately according to the type
Cons: After the upgrade, there is no way to cancel the upgrade, like the Windows patch can be installed, can be uninstalled principle, there is no research to understand, want to know the guidance of cattle people.
Of course, you can simply uninstall, then install, for the configuration files and other information special processing can also.
Of course, if you consider a different version of the client, you need to upgrade to the latest version, it is obvious that different versions of the corresponding upgrade files are different, it will be more complex, but the basic principle is unchanged.
4 Starting the main program
access to the main program path:
Relative path main program, update program, all use relative path, disadvantage is that once the relative path is determined, subsequent updates cannot change this directory relationship.
Registry paths are stored in the registry, when needed through the registry interaction, the main program to write the registry, the update program read the registry, the disadvantage is that the read and write registry requires permissions, write the path to be fixed, subsequent updates cannot change the location written in the registry, that is, the registry path.
Run Program code
[CSharp]View Plaincopy
- Private static void Runfile (string dir, string localfilename) {
- string info = "Run program" + LocalFilename;
- try{
- if (file.exists (Path.Combine (dir, LocalFilename))) {
- Process myprocess = new Process ();
- ProcessStartInfo psi = new ProcessStartInfo ();
- Psi. FileName = LocalFilename;
- Psi. WorkingDirectory = dir;
- Psi. UseShellExecute = false;
- Psi. Redirectstandarderror = true;
- Psi. CreateNoWindow = true;
- Psi. Redirectstandardoutput = true;
- Psi. WindowStyle = Processwindowstyle.hidden;
- Myprocess.startinfo = PSI;
- Myprocess.start ();
- String error = MyProcess.StandardError.ReadToEnd ();
- string output = MyProcess.StandardOutput.ReadToEnd ();
- Myprocess.waitforexit ();
- Myprocess.close ();
- if (Error! = string. Empty) {
- Log.write ("StandardError:" + error);
- }
- if (Output! = string. Empty) {
- Log.write ("standardoutput:" + output);
- }
- Log.logprocessend (info);
- }
- }
- catch (Exception ex) {
- Log.write (info + "error");
- Log.logexception (ex);
- throw ex;
- }
- }
- }
Source code Download
Reference articles
Reference Article 2
Reference Article 3
The principle and realization of automatic upgrade