CSDN博客

img amsdn

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

发表于2004/6/24 10:26:00  921人阅读

文件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

相关博文

我的热门文章

img
取 消
img