This example shares the specific code for Winform to use FTP to achieve automatic updates for your reference. The specific content is as follows
Implementation idea: Before opening the main program, determine whether it is necessary to update (you can record the version number through the database table or other methods to record whether it is required to update). If you need to update, download the update package from the ftp site (you can search for the ftp site yourself and not explain it in detail here). You can make a package with suffix format or something else! Generally, the latest program is stored in the form of a compressed package, download the file to the local path, close the current program and open the update program to perform decompression and replacement of the file, or you can use batch files or executable files to perform operations!
1. Determine whether there is a new version.
2. Download the update package to the local path through ftp.
3. Open the update program (batch file or executable file) and close all main program processes at the same time.
4. Decompress and replace operations in the update program.
5. Delete the local update package (optional).
6. Open the new program and close all update program processes at the same time.
Code:
1. Make judgments at the program entrance:
//Judge whether the version number is the version number of the database table if (edition == "2021.5.12.0.1")//You can judge the version number by yourself { var resulta = ("Are there any updates available, is it updated?", "System Prompt", , ); if (resulta == ) { (new Form1()); return; } //After the new compressed file from the server and download it to a certain path UpdatateHelp help = new UpdatateHelp();//Update class = ""; = ""; = "Administrator"; = "*****"; string message = ; if (!(out message)) { var result = (message, "System Prompt", , ); if (result == ) { (new Form1()); return; } else { //Forced shutdown of the process var proc = (().ProcessName); foreach (Process item in proc) { (); } } } //Replace the program file (using an update program is responsible for decompressing the program and replacing the file, deleting the compressed file) ( + "\\Update\\" + ""); //Close the current process foreach (Process item in (().ProcessName)) { (); } }
2. Update help class UpdateHelp
using System; using ; using ; using ; using ; using ; namespace OldDemo { class UpdatateHelp { /// <summary> /// Server IP /// </summary> public string IP { get; set; } /// <summary> /// The server file and download to the local file name are the same /// </summary> public string ServerFile { get; set; } /// <summary> /// Server username /// </summary> public string User { get; set; } /// <summary> /// Server Password /// </summary> public string Password { get; set; } /// <summary> /// Download the server file /// </summary> /// <param name="Message">Return information</param> /// <returns></returns> public bool DownloadFile(out string Message) { FtpWebRequest reqFTP; try { FileStream outputStream = new FileStream(()+ ServerFile, );//Local cache directory reqFTP = (FtpWebRequest)(new Uri("ftp://" + IP + "//"+ ServerFile)); = ;//Specify what command is the current request (upload, download, filelist, etc.) = true;//Specify the file transfer type = new NetworkCredential(User, Password); //Specify the user name and password for logging into the ftp server. = false;//Specify whether to close the control connection to the FTP server after the request is completed // = true;//Specify whether to use active mode or passive mode // = null;//Set not to use proxy // = 3000; FtpWebResponse response = (FtpWebResponse)(); Stream ftpStream = (); long cl = ; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = (buffer, 0, bufferSize); while (readCount > 0) { (buffer, 0, readCount); readCount = (buffer, 0, bufferSize); } (); (); (); Message = "Download the update file successfully!"; return true; } catch (Exception ex) { Message = "Download update file failed! Reason:"+ + "Press Yes to enter the original program, press No to close the program!"; return false; } } } }
3. Close the main program process and open the update program. You can execute it in the Program or create a new form to display the progress bar in the program, etc.! Here we use Form1 form for decompression processing. The thing to note is that I quoted using; you can search DotNetZip under Nuget. This dll is a helper class for decompression of files. This is just an example of decompression. If you are interested in studying other implementations yourself!
using System; using ; using ; using ; using ; using ; using ; using ; using ; using ; namespace AutoUpdate { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { StartUpdate(); } void StartUpdate() { string FileLoad = () + ""; string IndexLoad = + "\\"; var lis = ('\\').ToList(); (""); ("Update");//Because the update program is not synchronized with the main program directory, it needs to be returned to the main program directory at the first level in the Update folder. IndexLoad = ("\\",lis); //1. Unzip the temporary file package to the current path and delete the compressed package if ((FileLoad)) { = "Decompressing the software update package..."; //Disrepair if it exists if (!Decompression(FileLoad, IndexLoad, true)) { ("The unzipped update package failed, please try again!", "System Prompt"); //Close the current process foreach ( item in (().ProcessName)) { (); } } = "Removing software update package..."; //Delete the compressed package (FileLoad); } else { ("The software update package does not exist, please reopen the program to get the update package!", "System Prompt"); //Close the current process foreach ( item in (().ProcessName)) { (); } } = "Update successfully, please wait..."; //2. Open the updated program (IndexLoad + "\\" + ""); //Close the current process foreach ( item in (().ProcessName)) { (); } } /// <summary> /// Unzip the ZIP file /// </summary> /// <param name="strZipPath">ZIP file to be unzipped</param> /// <param name="strUnZipPath">Decompressed directory</param> /// <param name="overWrite">Whether to overwrite</param> /// <returns>Success: true/Failed: false</returns> public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite) { try { ReadOptions options = new ReadOptions(); = ;//Set encoding to solve the problem of Chinese garbled code when decompressing files using (ZipFile zip = (strZipPath, options)) { foreach (ZipEntry entry in zip) { if ((strUnZipPath)) { strUnZipPath = ('.').First(); } if (overWrite) { (strUnZipPath, );//Unzip the file, if it already exists, overwrite it } else { (strUnZipPath, );//Unzip the file, if it already exists, it will not be overwritten } } return true; } } catch (Exception) { return false; } } } }
4. Several things to pay attention to are:
4.1 Create a folder Update in the main program generation directory;
4.2 Put the update program generation file into the Update folder, mainly this section in the main program program (the main program directory and the update directory are not at the same level):
//Replace the program file (using an update program is responsible for decompressing the program and replacing the file, deleting the compressed file) ( + "\\Update\\" + "");
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.