C # Implementation of online update software

Source: Internet
Author: User
Tags foreach

WinForm programs relative to Web programs, more powerful, more convenient programming, but the software update is quite troublesome, to the client a platform to upgrade, in the face of this practical problem, in a recent small project, I designed a software to achieve automatic upgrade technology scheme to make up for this defect, Have a good reference value

Through some means to update the software background. First you need to have a website that places new versions of information

UpdateSoftwareForm.cs

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 02 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 Using System; Using System.Collections.Generic; Using System.ComponentModel; Using System.Data; Using System.Drawing; Using System.Linq; Using System.Text; Using System.Windows.Forms; Using Ccwin; Using System.Net; Using System.Collections; Using System.IO; Using System.Xml; Using System.Diagnostics; Using System.Threading;   namespace Writebook {public partial class Updatesoftwareform:skin_metro {public Updatesoftwareform () {Initiali Zecomponent ();   #region Some objects and variables  //use WebClient to download WebClient client = new WebClient (); ArrayList downlist = new ArrayList (); Current version string nowversion = null; Latest Version string latesversion = null;   #endregion   #region get version number  ///<summary>///Get the latest version number from the server///</summary> public void downl Oadcheckupdatexml () {try {///The first parameter is the address of the file, and the second parameter is the file-saved path file name, client. DownloadFile ("Http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WriteBook2.xml", "Writebook2.xml"); catch {MessageBox.Show ("No updates detected.) "," hint "); This. Close (); }  ///<summary>///get version number of local software///</summary> private void Nowversion () {nowversion = System.reflect Ion. Assembly.getexecutingassembly (). GetName (). Version.tostring () + "n"; Localtext.text = nowversion; }  ///<summary>///Read the latest version number obtained from the server///</summary> public void latestversion () {try {if (file.exists) (" WriteBook2.xml.xml ")) {XmlDocument doc = new XmlDocument ();//load XML doc to read. Load ("WriteBook2.xml.xml");  //Get root node XmlElement writebook = Doc. DocumentElement;  //Get the collection of node return nodes XmlNodeList Update = writebook.childnodes;   foreach (XmlNode item in Update) {latesversion = Item. InnerText; } latesttext.text = Latesversion; ' Else {MessageBox.Show (' No updates detected. "," hint "); This. Close (); } catch {this. Close (); }   #endregion   #region Initialization program  ///<summary>///initialization program///</summary> private void Initiali Zeandinstall () {updateprogressbar.value = 20; Downloadcheckupdatexml (); Updateprogressbar.value = 40; NowverSion (); Updateprogressbar.value = 60; Latestversion (); Updateprogressbar.value = 80; Downloadinstall (); Updateprogressbar.value = 100; }   #endregion   #region installation and removal  ///<summary>///Download installation package///</summary> public void Downloadi Nstall () {try {if (nowversion = = latesversion) {MessageBox.Show ("You are already the latest version.") "," prompt "); else if (nowversion!= latesversion && file.exists ("Writebook2.xml")) {MessageBox.Show ("Discover the new version, the update patch will be downloaded." "," hint ", MessageBoxButtons.OK, MessageBoxIcon.Information); Client. DownloadFile ("Http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WBsetup.exe", "WBsetup.exe"); if (file.exists ("Setup.exe")) {Installanddelete ();} else {for (int i = 1; i < 3; i++) {client. DownloadFile ("Http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WBsetup.exe", "WBsetup.exe"); MessageBox.Show ("Download failed, please check that your network connection is normal.") "," hint "); This. Close (); The catch {MessageBox.Show ("Update failed, no new version found. "," hint "); This. Close (); }  ///<summary>///installation and deletion///</summary> private void Installanddelete () {try {DialogResult Dr = MessageBox.Show ("Download update succeeded, do you want to install a new update?") "," hint ", messageboxbuttons.yesnocancel); if (dr = = System.Windows.Forms.DialogResult.Yes) {//Start the installer System.Diagnostics.Process.Start ("WBsetup.exe"); Thread td = New Thread (judgeinstall); Td. Start (); ' Else {} ' catch {MessageBox.Show ("Unknown error occurred, update failed. "," hint "); This. Close (); }  ///<summary>///determine if the installation process exists///</summary> public void Judgeinstall () {while (true) {process[] P Rocesslist = Process.getprocesses (); foreach (Process process in processlist) {if (process). ProcessName = = "WBsetup.exe") {} else {DialogResult Dr = MessageBox.Show ("Successful update, do you want to delete the installation package?") "," hint ", messageboxbuttons.yesno); if (dr = = System.Windows.Forms.DialogResult.Yes) {file.delete ("WBsetup.exe"); File.delete ("Writebook2.xml"); {}}}}   #endregion  ///<summary>///Click Initialization///</summary>///<param name= "Sender" >&lt ;/param>///<param name= "E" ></param> Private void Updatebutton_click (object sender, EventArgs e) {Initializeandinstall ();}} }

The above is the entire contents of this article, I hope you can enjoy.

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.