C# 下实现在线升级

转的,还没有测试

  1//这是一个webservice
  2
  3private AppUpdate.UpdateServ  UpdateSvr;
  4
  5
  6  private void button1_Click(object sender, System.EventArgs e)
  7  {
  8
  9   if(LinkWebServices()==true)
 10   {
 11    this.label1.Text=“连接服务器. PASS“;
 12
 13    if(CheckVer()==true)
 14    {
 15     this.label2.Text=“检查最新版本并下载.PASS“;
 16
 17    }

 18    else
 19    {
 20     this.label2.Text=“检查最新版本并下载.FAIL“;
 21    }

 22   }

 23   else
 24   {
 25    this.label1.Text=“连接服务器.FAIL“;
 26   }

 27  }

 28
 29//这是用来与升级服务器建立连接
 30  private bool LinkWebServices()
 31  {
 32   try
 33   {
 34    UpdateSvr=new UpdateServ();
 35    return true;
 36   }

 37   catch
 38   {
 39    return false;
 40   }

 41  }

 42
 43//调用webservice用来检查是不是有最新的版本
 44  private bool CheckVer()
 45  {
 46   string path =Application.StartupPath;
 47   try
 48   {
 49    VersionCheck(path);
 50    return true;
 51   }

 52   catch(Exception ex)
 53   {
 54    MessageBox.Show(ex.ToString());
 55    return false;
 56   }

 57  }

 58
 59  private void VersionCheck(string desPath)
 60  {
 61   try
 62   {
 63    查看文件和目录
 89
 90
 91    System.Xml.XmlDocument serverXmlDoc = UpdateSvr.AppUpdateVertion();
 92    System.Xml.XmlDocument localXmlDoc = new System.Xml.XmlDocument();
 93    localXmlDoc.Load(desPath + “UpdateConfig.xml“);
 94    bool newVersionExist = false;
 95    bool moduleExist = false;
 96    System.Xml.XmlNode serverNode0 = serverXmlDoc.ChildNodes[0];
 97    System.Xml.XmlNode localNode0 = localXmlDoc.ChildNodes[0];
 98    foreach(System.Xml.XmlNode serverNode in serverNode0)
 99    {
100     moduleExist = false;
101     foreach(System.Xml.XmlNode localNode in localNode0)
102     {
103      //找到对应模块
104      if(localNode.ChildNodes[0].InnerText == serverNode.ChildNodes[0].InnerText)
105      {
106       moduleExist = true;
107       //版本号判断
108       if(localNode.ChildNodes[1].InnerText.CompareTo(serverNode.ChildNodes[1].InnerText) 〈 0)
109       {
110        newVersionExist = true;
111        if(System.Configuration.ConfigurationSettings.AppSettings[“NetStyle“].ToString()==“internet“)
112        {
113         DownloadFile(serverNode.ChildNodes[2].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
114        }

115        else
116        {
117         DownloadFile(serverNode.ChildNodes[3].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
118        }

119       }

120       break;
121      }

122     }

123     //没找到对应模块
124     if(false == moduleExist)
125     {
126
127      if(System.Configuration.ConfigurationSettings.AppSettings[“NetStyle“].ToString()==“internet“)
128      {
129       DownloadFile(serverNode.ChildNodes[2].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
130      }

131      else
132      {
133       DownloadFile(serverNode.ChildNodes[3].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
134      }

135     }

136    }

137    //写入新UpdateConfig.xml升级完毕后替换
138    if(newVersionExist)
139    {
140     serverXmlDoc.Save(tempPath + “UpdateConfig.xml“);
141     if(DialogResult.Yes == MessageBox.Show(“有新版本,是否更新?“,“提示“,MessageBoxButtons.YesNo))
142     {
143      string[] dirs = System.IO.Directory.GetFiles(tempPath, “*.*“);
144      string fileName;
145      foreach (string dir in dirs)
146      {
147       fileName = ((dir.Split(Convert.ToChar(@“\“)))[dir.Split(Convert.ToChar(@“\“)).Length - 1]);
148       if(System.IO.File.Exists(desPath + fileName))
149       {
150        //TODO:可以支持备份以前版本
151        System.IO.File.Delete(desPath + fileName);
152       }

153       //TODO:如果系统正在运行,您得停止系统,至于如何停止,也许可以使用System.Diagnostics.Process
154       System.IO.File.Move(dir,desPath + fileName);
155      }

156      MessageBox.Show(“升级完毕“);
157     }

158     else
159     {
160      //TODO:可以支持重新提示升级
161     }

162    }

163   }

164   catch(Exception ex)
165   {
166    throw new Exception(“升级失败,原因是:“ + ex.Message,ex);
167   }

168  }

169
170//下载最新的文件
171
172  private void DownloadFile(string source,string fileName)
173  {
174   try
175   {
176    System.Net.WebClient myWebClient = new System.Net.WebClient();
177    myWebClient.DownloadFile(source,fileName);
178   }

179   catch(Exception ex)
180   {
181    throw new Exception(“下载失败,原因是:“ + ex.Message,ex);
182   }

183  }

184
185该文章转载自网络大本营:http://www.xrss.cn/Dev/DotNet/200722310638.Html
原文地址:https://www.cnblogs.com/Sabre/p/1195806.html