使用Web Services 来实现软件自动升级的实践

文件update.xml:

<?xml version="1.0" encoding="utf-8" ?>
<product>
<version>1.2</version>
<description>语音程序正式版发布</description>
<filelist count="2" sourcepath="./update/">
<item name="WindowsApplication2.exe" size="" >
<value />
</item>
<item name="Interop.SpeechLib.dll" size="">
<value />
</item>
</filelist>
</product>

服务端websvs.asmx代码:

namespace WebSvs
{
 [WebService(Namespace="http://10.16.80.22/WebSvs/")]

 public class UpdateSvs : System.Web.Services.WebService
 {
  //自动生成的代码略......

  [WebMethod(Description="取得软件最新版本信息")]
  public string GetVer()
  {
   XmlDocument doc = new XmlDocument();
   doc.Load(Server.MapPath("update.xml"));
   XmlElement root = doc.DocumentElement;

   return root.SelectSingleNode("version").InnerText;
  }

  [WebMethod(Description="在线更新软件")]
  public System.Xml.XmlDocument GetUpdateData()
  {
   //取得更新的xml模板内容
   XmlDocument doc = new XmlDocument();
   doc.Load(Server.MapPath("update.xml"));
   XmlElement root = doc.DocumentElement;
   //看看有几个文件需要更新
   XmlNode updateNode = root.SelectSingleNode("filelist");
   string path = updateNode.Attributes["sourcepath"].Value;
   int count = int.Parse(updateNode.Attributes["count"].Value);
   //将xml中的value用实际内容替换
   for(int i=0;i<count;i++)
   {
    XmlNode itemNode = updateNode.ChildNodes[i];
    string fileName = path + itemNode.Attributes["name"].Value;
    FileStream fs = File.OpenRead(Server.MapPath(fileName));
    itemNode.Attributes["size"].Value = fs.Length.ToString();
    BinaryReader br = new BinaryReader(fs);
    //这里是文件的实际内容,使用了Base64String编码
    itemNode.SelectSingleNode("value").InnerText = Convert.ToBase64String(br.ReadBytes((int)fs.Length),0,(int)fs.Length);
    br.Close();
    fs.Close();
   }
   return doc;
  }

 }
}

客户端主程序代码:

namespace WindowsApplication2
{
 //自动生成的代码略......
 public class Form1 : System.Windows.Forms.Form
 { 
  //先要添加web引用http://10.16.80.22/websvs/websvs.asmx,命名为zwt_svs
  private static zwt_svs.UpdateSvs upd ;
  
  //主程序的处理函数略......

  void update()
  {
   System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

   System.Xml.XmlNode tmpNode = upd.GetUpdateData();

   doc.AppendChild(doc.ImportNode(tmpNode,true));
   doc.Save(Application.StartupPath + @"/update.xml");
   
   System.Diagnostics.Process.Start(Application.StartupPath + @"/update.exe");
   Close();
   Application.Exit();
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   this.Text += Application.ProductVersion.ToString();

   try
   {
    upd = new zwt_svs.UpdateSvs();
    string nVer=upd.GetVer();
   
    //软件版本号在AssemblyInfo.cs中指定
    if(Application.ProductVersion.CompareTo(nVer)<0)
     update();
   }
   catch (Exception ex)
   {
    MessageBox.Show("发生异常:"+ex.Message);
    Close();
    Application.Exit();
   }
  }

 }
}


更新程序update.cs代码:

namespace update
{
 public class UpdateForm : System.Windows.Forms.Form
 {
  //自动生成的代码略...... 
  Thread tid;

  private void DoUpdate()
  {
   Process[] myProcesses ;

   try
   {
    myProcesses = Process.GetProcessesByName("windowsapplication2");

    if(myProcesses.Length > 0)
    {
     foreach(Process myProcess in myProcesses)
     {
      myProcess.Kill();
     }
    }

    XmlDocument doc = new XmlDocument();
    doc.Load(Application.StartupPath + @"/update.xml");

    XmlElement root = doc.DocumentElement;
    XmlNode updateNode = root.SelectSingleNode("filelist");

    string path = updateNode.Attributes["sourcepath"].Value;
    int count = int.Parse(updateNode.Attributes["count"].Value);
   
    for(int i=0;i<count;i++)
    {
     XmlNode itemNode = updateNode.ChildNodes[i];
     string fileName = itemNode.Attributes["name"].Value;
     FileInfo fi = new FileInfo(fileName);
     fi.Delete();
    
     this.label1.Text = "正在更新: " + fileName + " (" + itemNode.Attributes["size"].Value + ") ...";
     FileStream fs = File.Open(fileName,FileMode.Create,FileAccess.Write);
     fs.Write(System.Convert.FromBase64String(itemNode.SelectSingleNode("value").InnerText),0,int.Parse(itemNode.Attributes["size"].Value));
     fs.Close();
    }

    label1.Text = "更新完成";
    File.Delete(Application.StartupPath + @"/update.xml");
    label1.Text = "正在重新启动应用程序...";

    System.Diagnostics.Process.Start("WindowsApplication2.exe");
    Close();

    Application.Exit();
   }
   catch (Exception ex)
   {
    MessageBox.Show("Caught Exception .... : " + ex.Message);
    Close();
    Application.Exit();
   }     
     
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   //新起一个线程进行程序更新
   tid = new Thread(new ThreadStart(this.DoUpdate));
   tid.Start();
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   tid.Abort();
   Close();
   Application.Exit();
  }
  
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值