应朋友要求,需要一个C# winform的自动更新要求 本来想想很简单 但实际操作也弄了一个下午(可能技术水平问题) 话不多说代码奉上。如有需要Email:zhulong1111@yahoo.cn 现给新手学习
主界面
thread+delegate实现信息提示
下载的文件(实际下载下来还要对文件解压,覆盖。。这些小功能就没去写了)
主页代码:
- C# code
public partial class Form1 : Form { Thread th = null; public Form1() { InitializeComponent(); } public delegate void SetText(string text,int i); //声明委托 /// <summary> /// 委托实现 /// </summary> /// <param name="value">值</param> /// <param name="i">1表示为本地版本控件,2表示服务器版本控件,3表示提示信息控件</param> public void setTextVlue(string value,int i) { if (i == 1) lblOld.Text = value; else if (i == 2) lblNew.Text = value; else if (i == 3) lblMessage.Text = value; else if (i == 4) button1.Enabled = true; } /// <summary> /// 窗体加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { th = new Thread(new ThreadStart(threadFun)); th.Start(); button1.Enabled = false; } public void threadFun() { lblMessage.Invoke(new SetText(setTextVlue), "获取本地版本号。。。",3); string enition = GetSourceList(System.AppDomain.CurrentDomain.BaseDirectory + "UpXml.xml", "Version")[0]; lblOld.Invoke(new SetText(setTextVlue),enition,1); Thread.Sleep(2000); lblMessage.Invoke(new SetText(setTextVlue),"获服务器地版本号。。。",3); Thread.Sleep(2000); string url = GetSourceList(System.AppDomain.CurrentDomain.BaseDirectory + "UpXml.xml", "url")[0]; enition = GetSourceList(url, "Version")[0]; lblNew.Invoke(new SetText(setTextVlue),enition,2); lblMessage.Invoke(new SetText(setTextVlue), "数据获取结束", 3); button1.Invoke(new SetText(setTextVlue), "打开按钮状态", 4); } /// <summary> /// 读取xml获取数据 /// </summary> /// <param name="xmlfilePath">xml路径</param> /// <param name="nodeName">点名称</param> /// <returns>xml值</returns> public static List<string> GetSourceList(string xmlfilePath, string nodeName) { XmlTextReader textReader = null; List<string> listStr = new List<string>(); try { textReader = new XmlTextReader(xmlfilePath); while (textReader.Read()) { if (textReader.NodeType == XmlNodeType.Element && textReader.IsStartElement()) { if (textReader.Name == nodeName) { listStr.Add(textReader.ReadInnerXml()); } } } } catch(Exception ee) { textReader.Close(); listStr.Add(ee.Message); } return listStr; }
下载页面代码
- C# code
private void DownList_Load(object sender, EventArgs e) { dt = getSource(); dataGridView1.DataSource = dt; } /// <summary> /// 转化为DateTable 用于绑定 /// </summary> /// <returns></returns> public DataTable getSource() { string url = Form1.GetSourceList(System.AppDomain.CurrentDomain.BaseDirectory + "UpXml.xml", "url")[0]; List<string> list = Form1.GetSourceList(url, "UpdateFile"); DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; string url2 = Form1.GetSourceList(url, "ut")[0]; //赋值给dc,是便于对每一个datacolumn的操作 dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); dc.AutoIncrement = true;//自动增加 dc.AutoIncrementSeed = 1;//起始为1 dc.AutoIncrementStep = 1;//步长为1 dc.AllowDBNull = false;// dc = tblDatas.Columns.Add("name", Type.GetType("System.String")); dc = tblDatas.Columns.Add("lenght", Type.GetType("System.String")); dc = tblDatas.Columns.Add("url", Type.GetType("System.String")); DataRow newRow; for (int i = 0; i < list.Count; i++) { newRow = tblDatas.NewRow(); newRow["name"] = list[i]; newRow["lenght"] = list[i].ToString().Length; newRow["url"] =url2+"//"+ list[i].ToString(); tblDatas.Rows.Add(newRow); } return tblDatas; } //设置进度条 public void setProgressBar() { progressBar1.Value = 0; progressBar1.Minimum = 0; progressBar1.Maximum = dt.Rows.Count; progressBar1.Step = 1; for (int i = 0; i < dt.Rows.Count; i++) { progressBar1.Value += 1; WebClient client = new WebClient(); client.DownloadFile(dt.Rows[0]["url"].ToString(), dt.Rows[i]["name"].ToString()); Thread.Sleep(1000); } } private void button1_Click(object sender, EventArgs e) { setProgressBar(); }