Compile the WinForm automatic update program

Source: Internet
Author: User

This article from: http://www.cnblogs.com/sdlfx/archive/2010/11/25/1888207.html

 

Maybe I haven't updated my blog for a year or two. It's not because I don't have time or I don't have any technical experience during this time, but I feel that I can't write my blog every time I open it. Maybe it's because you haven't got any exercise.

Low. Therefore, we decided to use logs for training from today on. Document, I hope it will not be so painful next time.
 
Start with writing the WinForm automatic update program.
       
For C/S architecture projects, the client will inevitably face automatic updates, especially when there are a large number of clients, automatic updates are particularly important. As far as I know, there are two ways to automatically update the WinForm program:
1. Use ClickOnce to publish the client;
2. Write your own automatic update program;
  
In view of the "Laziness" principle, I tested ClickOnce deployment. After some twists and turns, it was successful, but I still feel that there are several disadvantages:
A. Release is inconvenient. Once released, you need to generate a program list and a subordinate list. The generated files are completely unusable and have to be reused. There seems to be a useless. deploy;
B. The installation directory cannot be specified. I didn't find the installation directory that is automatically located, and it should be quite hidden;
C. No progress is displayed during the update. This affects the user experience. If the update fails, the generated logs may not be known for development;
There is also a small disadvantage. Starting installation on a browser always feels a little weird ....
 
Then, start to drum up an automatic update program. There are several problems in the design process that must be selected:
 
1. How can I access the update source? You can provide an update source through the Development file reading service. You can use FTP as the update source, file sharing, and WEB Sharing. I have selected
Use FTP as the update source;
2. Based on what to determine whether the file needs to be updated? The first thing that comes to mind is the version number. Except for DLL and EXE files, most files do not have version numbers. What is the content comparison? That is even worse. I used
To compare. Each file can generate a unique HASH code. As long as the file has been changed, the generated HASH code will be different.
 
General idea of the program: Provide an independent DLL for the main program to call, pass in FTP access credenftp, start the update program, and close the main program. The update program first downloads a file list from FTP, columns in the file list
Lists all the files and directories in the new version, and saves the HASH of each file. After downloading the file, compare it with the HASH of the local file in sequence. If different, download the file and update it. After the traversal is complete, start the main program and attach
Add parameters to notify the called DLL that the update has been completed to prevent endless loops and finally exit the update program. The following are some technical points:
 
File HASH code generation
        
    public static string GetMD5(Stream stream)    {        byte[] buffer;        using (HashAlgorithm hash = HashAlgorithm.Create())        {            buffer = hash.ComputeHash(stream);            hash.Clear();        }        return Convert.ToBase64String(buffer);    }

 

The HashAlgorithm class is provided in the. NET Framework and is in the System. Security. Cryptography namespace. Here is something I don't quite understand: use File. IsExist to determine whether the File exists.

If "\" is added before the passed File name parameter, the returned result is always true, regardless of whether the File exists, such as File. isExist (@ "\ temp. xml). What's more strange is that you can use this path to generate a HASH code.

Passing parameters between applications

When writing the console program, the automatically generated Main method contains the string [] args parameter, which is the parameter passing entry. In addition, you can use the Process class to start the application, for example:

    Process.Start(Application.StartupPath + "\\Update.exe", arguments);

Arguments is a string. When multiple parameters are passed, the parameters are separated by spaces. If a single parameter contains spaces, enclose the parameter in double quotation marks. When passing parameters, note that "\" may correspond

The character composition following it forms an escape. If you find that the number of parameters is incorrect when obtaining the parameters in Main, you should check this.

 

There seems to be nothing to say about the rest. It's all about physical activity. As for FTP access, I used a class provided by a previous friend, and I have not studied its details. When displaying the update progress, remember to use Form. Invoke

Update the form rendering.

 

When using this automatic update program, first use the Release project to generate the project file list, and then compare the project file list with the generated FileList. the xml file is stored in an FTP directory, and the accessed user is configured.

After the name and password, you can use the following statements in the main program to automatically update the password:

FtpInfo ftpInfo = new FtpInfo ("10software.gicp.net", "\ test", "ftpclient", "p @ ssw0rd", 21 );

Lib. Update. Pipe. Update (ftpInfo, UpdateBaseMode. ByFileHash );

 

FTP is configured under Server2008, but it is successful after all. The next article will briefly introduce FTP configuration under Server2008.

 

Program source code: Updater.rar

 

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.