C # automatic online software upgrade Program

Source: Internet
Author: User

C # automatic online software upgrade Program

Source: http://www.longtengwang.com/Article/soft/Csharp/csharpSL/200801/8014.html

1 Preface

For a long time, the majority of programmers have been arguing over whether to use Client/Server or browser/server structures. Among these arguments, C/S structures have poor program maintainability and difficult arrangement, the upgrade is inconvenient, and the high maintenance cost is a very important factor. Many enterprise users give up using C/S for this reason. However, when an application must use the C/S structure to implement its functions well, how can we solve the problem of client deployment and automatic upgrade? Deployment is simple. You only need to click the installer. The difficulty is that automatic upgrade can be implemented whenever a new version is released [3]. Now, our goal is simple. We hope to develop a reusable automatic upgrade system that is irrelevant to specific applications. Below I provide you with a reusable automatic upgrade system written in C.

 

2. Difficulties in implementing automatic software upgrade

 

First, in order to find updates on the remote server, the application must have a way to query the network, which requires network programming and simple protocol for communications between the application and the server.

The second is download. It seems that you do not need to consider the Internet of Things, but you need to consider downloading the files requested by the user and downloading large files without the user's consent. The friendly automatic update application will use the remaining bandwidth to download updates. This sounds simple, but it is a technical problem. Fortunately, there is a solution.

The third factor is the process of replacing the original application with the new application. This problem is interesting because it requires that you delete yourself from the system during code runtime. There are multiple ways to implement this function [5], in this article, we mainly use the date number of the new and old versions to replace the new version of the application.

 

3. Principle of automatic online software upgrade

 

Write two programs, one is the main program; the other is the Upgrade Program; all the upgrade tasks are completed by the Upgrade Program.

 

1. Start the Upgrade Program, connect the Upgrade Program to the website, and download the new main program (including the supported library files and xml configuration documents) to the Temporary Folder;

 

2. the Upgrade Program obtains the update date, version number, or file size of the new version program in the XML configuration file on the server;

3. the upgrade program obtains the latest update date, version number, or file size of the original client application, and compares the two. if the date of the Upgrade Program is found to be later than the latest date of the original program, it indicates whether the user is upgrading, or compares the existing version with the latest version. If the latest version is found, it indicates whether the user is upgrading. Other attributes, such as the file size, are also used for comparison, if the file size of the Upgrade Program is larger than that of the old version, you are prompted to upgrade the program. This article mainly uses the updated date number of the new and old versions to prompt users to upgrade.

4. If the user chooses to upgrade, the system obtains the list of downloaded files and starts to download documents in batches;

5. the Upgrade Program checks whether the old master program is active. if the activity is active, the old master program is closed;

6. Delete the old main program and copy the files in the Temporary Folder to the corresponding location;

 

7. Check the status of the main program. If the status is active, start the new main program;

8. Close the upgrade process and complete the upgrade [4].

 

4. Key Steps for online upgrade using C #

Here, I mainly use date information to check whether the upgraded version needs to be downloaded.
4.1 prepare an xml configuration file
The name is autoupdater. xml. It serves as an upgrade template and displays the information to be upgraded.

 

<? XML version = "1.0"?> // XML version number
<Autoupdater>
<Urladdres url = "http: // 192.168.198.113/vbroker/log/"/> // URL of the server where the file is updated
<Updateinfo>
<Updatetime date = "2005-02-02"/> // update date of the update File
<Version num = "1.0.0.1"/> // version of the update File
</Updateinfo>
<Updatefilelist> // list of update files
<Updatefile filename = "aa.txt"/> // three files must be upgraded.

<Updatefile filename = "vb40.rar"/>
<Updatefile filename = "VB4-1.CAB"/>
</Updatefilelist>
<Restartapp>
<Restart allow = "yes"/> // You can restart the application.
<Appname name = "tims.exe"/> // name of the started Application
</Restartapp>
</Autoupdater>

From the above XML document you can know that the upgrade document server address, upgrade document update date, the list of files to be upgraded, a total of three files need to be upgraded: aa.txt0000vb40.rar, VB4-1.CAB. And whether the application can be restarted and the name of the application to be restarted.

4.2 obtain the last update date of the client application and server upgrade Program
It is implemented through the getthelastupdatetime () function.

 

 

Private string getthelastupdatetime (string DIR)
{
String lastupdatetime = "";
String autoupdaterfilename = dir + @ "" autoupdater. xml ";
If (! File. exists (autoupdaterfilename ))
Return lastupdatetime;
// Open the XML file
Filestream myfile = new filestream (autoupdaterfilename, filemode. Open );
// XML file Reader
Xmltextreader xml = new xmltextreader (myfile );
While (XML. Read ())
{
If (XML. Name = "updatetime ")
{
// Obtain the last update date of the upgrade document
Lastupdatetime = xml. getattribute ("date ");
Break;
}
}
XML. Close ();
Myfile. Close ();
Return lastupdatetime;
}

 

 
Use xmltextreader to open the XML document and read the update time to obtain the value corresponding to the date, that is, the last update time of the server-side Update file.

Function call implementation:
// Obtain the Last Update Time of the application in the specified path of the client.
String thepreupdatedate = getthelastupdatetime (application. startuppath );
Application. startuppath indicates the path of the client application.

// Obtain the last update date of the downloaded documents from the server
String thelastsupdatedate = getthelastupdatetime (thefolder. fullname );
Thefolder. fullname indicates the path of the Temporary Folder downloaded from the upgrade document to the client.

4.3 comparison date
The last update date of the client application is compared with the last update date of the server upgrade program.

// Obtain the latest update date of the downloaded document
String thelastsupdatedate = getthelastupdatetime (thefolder. fullname );
If (thepreupdatedate! = "")
{
// If the client updates the upgraded application on a date greater than the update date of the application on the server
If (convert. todatetime (thepreupdatedate)> = convert. todatetime (thelastsupdatedate ))
{
MessageBox. Show ("the current software is up to date, no updates required! "," System prompt ", messageboxbuttons. OK, messageboxicon. information );
This. Close ();
}
}
This. labdownfile. Text = "download an Update file ";
This. labfilename. Refresh ();
This. btncancel. Enabled = true;
This. progressbar. Position = 0;
This. progressbartotal. Position = 0;
This. progressbartotal. Refresh ();

This. progressbar. Refresh ();

// Obtain the list of downloaded files through dynamic array
Arraylist list = getdownfilelist (gettheupdateurl (), thefolder. fullname );
String [] URLs = new string [list. Count];
List. copyto (URLs, 0 );

 

Compare the date of the client-upgraded application with the date of the application downloaded from the server. If the former is greater than the latter, the application is not updated. If the former is smaller than the latter, you can use a dynamic array to obtain the list of downloaded files and start downloading the files.

Implemented through the batchdownload () function. The upgrade program checks whether the old master program is active. If it is active, it closes the old master program. It deletes the old master program and copies the files in the Temporary Folder to the corresponding location; check the status of the main program. If the status is active, start the new main program.

 

 

Private void batchdownload (Object Data)
{
This. Invoke (this. activestatechanger, new object [] {true, false });
Try
{
Downloadinstructions instructions = (downloadinstructions) data;
// Batch download
Using (batchdownloader BDL = new batchdownloader ())
{
BDL. currentprogresschanged + = new downloadprogresshandler (this. singleprogresschanged );
BDL. statechanged + = new downloadprogresshandler (this. statechanged );
BDL. filechanged + = new downloadprogresshandler (bdl_filechanged );
BDL. totalprogresschanged + = new downloadprogresshandler (bdl_totalprogresschanged );
BDL. Download (instructions. URLs, instructions. Destination, (manualresetevent) This. cancelevent );
}
}
Catch (exception ex)
{
Showerrormessage (Ex); bitscn_com
}
This. Invoke (this. activestatechanger, new object [] {false, false });
This. labfilename. Text = "";
// Update the program
If (this. _ update)
{
// Close the original application
This. labdownfile. Text = "Closing the program ....";
System. Diagnostics. Process [] Proc = system. Diagnostics. process. getprocessesbyname ("TIMS ");
// Close all processes of the original application
Foreach (system. Diagnostics. Process Pro In Proc)
{
Pro. Kill ();
}
Directoryinfo thefolder = new directoryinfo (path. gettemppath () + "jurassicupdate ");
If (thefolder. exists)
{
Foreach (fileinfo thefile in thefolder. getfiles ())
{
// If the Temporary Folder contains a file with the same name as the file in the application directory, delete the file in the application directory.
If (file. exists (application. startuppath + "+ path. getfilename (thefile. fullname )))
File. Delete (application. startuppath + "+ path. getfilename (thefile. fullname ));
// Move the files in the Temporary Folder to the directory where the application is located
File. Move (thefile. fullname, application. startuppath + "" + path. getfilename (thefile. fullname ));
}
}
// Start the installer
This. labdownfile. Text = "starting the program ....";
System. Diagnostics. process. Start (application. startuppath + "+" tims.exe ");
This. Close ();
}
}
 

This program is the key code to implement online upgrade. The steps are a bit complicated: first, use the invoke method to synchronously call the process to change the status, and then call the dynamic link library for batch download (batchdownloader. CS) class starts batch download, and then determines whether the original main application is closed. If not, use process. kill () to close the main program. Next, check whether there is a file with the same name as the file in the Temporary Folder (that is, the directory where the update program is downloaded). If there is a file with the same name, delete the files in the application directory, and move the files in the Temporary Folder to the directory where the application is located. Restart the main application. This completes the update.

 

References

 

[1] Pang lan. 8051 SCM online upgrade software method. http://www.mcublog.com/more.asp? Name = mcublog & id = 3111. China Electronic Engineer blog site
[2] using Web Services in winform to implement auto update (C #). Http://www.cnblogs.com/x369/articles/105656.html. Blog
[3] reusable automatic upgrade system implementation. Http://www.cnblogs.com/cdo/archive/2005/09/06/231229.html.blog
[4] use VB6.0 to compile a self-Upgrade Program. Http://www.enet.com.cn/article/2004/0525/A20040525311583.shtml
[5]. Net Background Intelligent Transmission Service to achieve automatic update (on). http://www.itonline.gd.cn/news/detail.asp? News_id = 4071.

 

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.