C # tcp automatic update program,

Source: Internet
Author: User

C # tcp automatic update program,

The. NETTCP automatic update procedure involves the following steps:

Step 1: Enable listening on the server

ServiceHost host; private void button1_Click (object sender, EventArgs e) {host = new ServiceHost (typeof (WCFService. service); host. open (); if (host. state = CommunicationState. opened) {this. button1.Enabled = false; this. button2.Enabled = true;} this. label1.Text = host. state. toString ();}View Code

Step 2: create a service connection on the client.

Step 3: the client selects the Update file (including a Read_File)

# Region select file button private void btn_SelectFiles_Click (object sender, EventArgs e) {dt_updatefile.Clear (); OpenFileDialog File_XuanZe = new OpenFileDialog (); File_XuanZe.Multiselect = true; // multiple files can be selected simultaneously. File_XuanZe.Filter = "Dll files (*. dll) | *. dll | Txt files (*. txt) | *. txt | Access files (*. mdb) | *. mdb | All files (*. *) | *. * "; if (DialogResult. OK = File_XuanZe.ShowDialog () {for (int I = 0; I <bytes; I ++) {byte [] buff = Read_File (File_XuanZe.FileNames [I]); // For the third column, set a textbox as the version number. // For the fourth column, store the path sample in the image folder. the executable file is in dt_updatefile.Rows.Add (I + 1, file_XuanZe.SafeFileNames [I], txt_version.Text, @ "\ image \", buff) ;}} dataGridView1.DataSource = dt_updatefile ;} # endregion # region converts a file to byte [] Read_File public byte [] Read_File (string str_path) {byte [] arrFile = null; // define a byte array using (FileStream fs = new FileStream (str_path, FileMode. open, FileAccess. read) // path is the file path {arrFile = new byte [fs. length]; // defines the length of the byte [] array as the Length fs of the file. read (arrFile, 0, arrFile. length); // read the fs file into the arrFile array. 0 refers to the offset, which is read from 0 and arrFile. length refers to the length to be read, that is, the length of the entire file return arrFile; }}# endregionView Code

Step 4: Upload the updated file after data serialization and Compression

# Region Upload File button private void btn_UpdateFile_Click (object sender, EventArgs e) {DataSet ds = new DataSet (); if (dt_updatefile.DataSet! = Null) {partition (dt_updatefile);} ds. Tables. Add (dt_updatefile); client. Update_System (partition (ds); Load_OldFiles (); dt_updatefile.Clear ();} # endregionView Code

Step 5: The Server accepts the data and writes a file to modify the version information in XML.

# Region receives the update packet Update_System public void Update_System (byte [] byte_files) {DataSet ds = data_serializer.getzipbytessurrow.dataset (byte_files) from the client. // decompress the package into a dataset circular file, modify the corresponding value DataTable dt = ds in Version_Config.xml. tables [0]; foreach (DataRow dr in dt. rows) {string path = System. appDomain. currentDomain. baseDirectory + @ "\" + dr ["Col_Version"] + dr ["Col_File_Path"]; if (! Directory. exists (path) {Directory. createDirectory (path);} if (File. exists (path + dr ["Col_File_Name"]. toString () {// File exists. delete (path + dr ["Col_File_Name"]. toString () ;}# duplicate files are not excluded from region write-back files. // check whether the file exists. If the file exists, change it to the original file name + year, month, day, hour, minute, and second, if the file does not exist, write byte [] buffer = (byte []) dr ["Col_File_Size"]; FileStream fs = new FileStream (path + dr ["Col_File_Name"]. toString (), FileMode. create, FileAccess. write); Stream stream = new MemoryStream (byte []) dr ["Col_File_Size"]); int count = 0; while (count = stream. read (buffer, 0, buffer. length)> 0) {fs. write (buffer, 0, count);} // clear the buffer fs. flush (); // close the stream fs. close (); # endregion // The file is written. Now, the configXml value is modified. update_Xml (dr ["Col_File_Name"]. toString (), dr ["Col_Version"]. toString (), dr ["Col_File_Path"]. toString () ;}# endregionView Code

Step 6: the client detects the version number and updates the program.

# Region download file btn_DownFiles_Click private void btn_DownFiles_Click (object sender, EventArgs e) {System. xml. xmlDocument xDoc = new System. xml. xmlDocument (); xDoc. load (System. windows. forms. application. executablePath + ". config "); System. xml. xmlNode xNode; System. xml. xmlElement xElem1; xNode = xDoc. selectSingleNode ("// appSettings"); xElem1 = (System. xml. xmlElement) xNode. selectSingleNode ("// add [@ key = 've Rsion '] "); DataSet ds = data_serializer.getzipbytessurrow.dataset (client. downFiles (ConfigurationManager. appSettings ["version"]); DataTable dt_file = ds. tables [0]; // linq Double maxSV = Creat_Version (); foreach (DataRow dr in dt_file.Rows) {string path = System. appDomain. currentDomain. baseDirectory + dr ["Col_File_Path"]; if (! Directory. exists (path) {Directory. createDirectory (path);} if (File. exists (path + dr ["Col_File_Name"]. toString () {// File exists. delete (path + dr ["Col_File_Name"]. toString () ;}# duplicate files are not excluded from region write-back files. // check whether the file exists. If the file exists, change it to the original file name + year, month, day, hour, minute, and second, if the file does not exist, write byte [] buffer = (byte []) dr ["Col_File_Size"]; FileStream fs = new FileStream (path + dr ["Col_File_Name"]. toString (), FileMode. create, FileAccess. write); Stream stream = new MemoryStream (byte []) dr ["Col_File_Size"]); int count = 0; while (count = stream. read (buffer, 0, buffer. length)> 0) {fs. write (buffer, 0, count);} // clear the buffer fs. flush (); // close the stream fs. close (); # endregion} // the file has been written. Now, the xml value xElem1.SetAttribute ("value", maxSV. toString (); xDoc. save (System. windows. forms. application. executablePath + ". config ") ;}# endregionView Code

The preceding steps are generally used. The specific information such as classes is not posted in the code. You can download the source code for research.

Defects:

First, the client call does not determine whether the service is enabled.

Second, the file storage path is not complete yet.

Third: I did not think of my brain for the moment.

Download https://yunpan.cn/cSbKnajf63ptA access password 42ac

 

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.