C # Automatic Update design

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Xml;
Using System.Net;
Namespace AutoUpdate
{
public class Autoupdateconfig
{
public string Autoupdateurl {get; set;}
public string Version {get; set;}
public string Description {get; set;}
public int FileCount {get; set;}
public string FilePath {get; set;}
Public list<autoupdatefile> filelist {get; set;}
}
public class Autoupdatefile
{
<summary>
Filename
</summary>
public string FileName {get; set;}
<summary>
File size
</summary>
public string FileSize {get; set;}
<summary>
True indicates a successful download
</summary>
public string Status {get; set;}

}
public class AutoUpdate
{
Public Autoupdateconfig Getautoupdateconfig (string autoupdateurl)
{

Autoupdateconfig model = NULL;
XmlDocument doc = new XmlDocument ();
Try
{
Doc. Load (New WebClient (). OpenRead (Autoupdateurl));
}
catch (Exception ex)
{
DevExpress.XtraEditors.XtraMessageBox.Show (ex. message);
return model;
}
Model = new Autoupdateconfig ();
XmlElement root = Doc. DocumentElement;
Model. Version = root. selectSingleNode ("version"). Innertext.trim ();
Model. Description = root. selectSingleNode ("description"). InnerText;
Model.autoupdateurl = Autoupdateurl;
list<autoupdatefile> list = new list<autoupdatefile> ();
foreach (XmlNode node in root). selectSingleNode ("FileList"). ChildNodes)
{
Autoupdatefile item = new Autoupdatefile ();
item.filename = node. attributes["Name"]. InnerText;
item.filesize = node. attributes["Size"]. InnerText;
Item.status = "";
List. ADD (item);
}
Model.filelist = list;
Model.filecount = list. Count;
return model;
}
public void FormatVersion (ref string ver1, ref string ver2)
{
Ver1 = Ver1. Replace (".", "");
Ver2 = Ver2. Replace (".", "");
if (ver1. Length < Ver2. Length)
{
Ver1 = Ver1. PadRight (ver2. Length, ' 0 ');
}
Else
{
Ver2 = Ver2. PadRight (Ver1. Length, ' 0 ');
}

}

}
}

=============================frmmain.cs

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 System.Xml;
Using System.Net;
Using System.Diagnostics;
Using System.Reflection;
Namespace AutoUpdate
{
public partial class FrmMain:DevExpress.XtraEditors.XtraForm
{
AutoUpdate dal = new AutoUpdate ();
Autoupdateconfig model;
String Autoupdateurl {get; set;}
String ProcessName {get; set;}
Public Frmmain (String autoupdateurl, String processname)
{
InitializeComponent ();

This.autoupdateurl = Autoupdateurl;
This.processname = ProcessName;
}

        private void Frmmain_load (object sender, EventArgs e)
  & nbsp;     {
            Txtdescription.visible = true;
            gridcontrol1.visible = false;
            btnupdate.text = "Next";
            model = dal. Getautoupdateconfig (Autoupdateurl);

if (model = = NULL)
{
btnupdate.enabled = false;
Txtdescription.text = autoupdateurl+ "\ r \ n Remote server error";

}
Else
{
Txtdescription.text = model. Description;
String version = Assembly.getexecutingassembly (). GetName (). Version.tostring ();
string ver1 = version;
String ver2 = model. Version;
Dal.formatversion (ref ver1, ref ver2);
if (Convert.ToInt32 (ver1) >= Convert.ToInt32 (ver2))
{
DevExpress.XtraEditors.XtraMessageBox.Show ("Current is the latest version no need to update", "hint");
}
if (model!= null)
{
Gridcontrol1.datasource = model.filelist;
}
}
}
void Client_downloadfilecompleted (object sender, AsyncCompletedEventArgs e)
{
Autoupdatefile item = (autoupdatefile) e.userstate;
if (e.error!= null)
{
Item.status = "Download Failed";
Gridview1.refreshdata ();
}
Else
{
Model.fileList.FirstOrDefault (U => u.filename = = item.filename). Status = "Success";
Item.status = "Success";
Gridview1.refreshdata ();
if (Model.fileList.FirstOrDefault (U => u.status!= "success") = = null)
{
DevExpress.XtraEditors.XtraMessageBox.Show ("has successfully upgraded to" + model.) Version + ", please reopen the WMS system", "prompt");
This. Text = "Successfully upgraded to" + model. Version;
Btnupdate.visible = false;
}
}
}
void Client_downloadprogresschanged (object sender, DownloadProgressChangedEventArgs e)
{
Autoupdatefile item = (autoupdatefile) e.userstate;
Item.status= (E.bytesreceived/convert.todecimal (e.totalbytestoreceive)). ToString ("P");
Gridview1.refreshdata ();
}
private void Btnupdate_click (object sender, EventArgs e)
{
if (txtdescription.visible)
{
if (Process.getprocessesbyname (this.processname). Length > 0)
{
DevExpress.XtraEditors.XtraMessageBox.Show ("Please close the WMS Main system", "warning");
Return
}

Gridcontrol1.visible = true;
Txtdescription.visible = false;
Btnupdate.text = "Upgrade";

}
Else
{
for (int i = 0; i < Gridview1.rowcount; i++)
{
Autoupdatefile item = (autoupdatefile) gridview1.getrow (i);
using (WebClient client = new WebClient ())
{
string fileName = Item.filename;
Client. DownloadFileAsync (new Uri (new Uri (Model.autoupdateurl), filename), filename, item);
Client. DownloadProgressChanged + = new Downloadprogresschangedeventhandler (client_downloadprogresschanged);
Client. downloadfilecompleted + = new Asynccompletedeventhandler (client_downloadfilecompleted);
}
}
}

}

private void Btnexit_click (object sender, EventArgs e)
{
This. Close ();
}


}
}


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.