日期:2011-06-26  浏览次数:20444 次

//这是一个webservice

private AppUpdate.UpdateServ  UpdateSvr;


  private void button1_Click(object sender, System.EventArgs e)
  {

   if(LinkWebServices()==true)
   {
    this.label1.Text=“连接服务器....... PASS“;

    if(CheckVer()==true)
    {
     this.label2.Text=“检查最新版本并下载.......PASS“;

    }
    else
    {
     this.label2.Text=“检查最新版本并下载.......FAIL“;
    }
   }
   else
   {
    this.label1.Text=“连接服务器.......FAIL“;
   }
  }

//这是用来与升级服务器建立连接
  private bool LinkWebServices()
  {
   try
   {
    UpdateSvr=new UpdateServ();
    return true;
   }
   catch
   {
    return false;
   }
  }

//调用webservice用来检查是不是有最新的版本
  private bool CheckVer()
  {
   string path =Application.StartupPath;
   try
   {
    VersionCheck(path);
    return true;
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.ToString());
    return false;
   }
  }

  private void VersionCheck(string desPath)
  {
   try
   {
    #region 查看文件和目录
    if(!desPath.EndsWith(@“\“))
     desPath += @“\“;

    if(!System.IO.Directory.Exists(desPath))
    {
     System.IO.Directory.CreateDirectory(desPath);
    }

    string tempPath = desPath + @“tempDesPathCache\“;

    if(System.IO.Directory.Exists(tempPath))
    {
     System.IO.Directory.Delete(tempPath,true);
     System.IO.Directory.CreateDirectory(tempPath);
    }
    else
     System.IO.Directory.CreateDirectory(tempPath);

    if(!System.IO.File.Exists(desPath + “UpdateConfig.xml“))
    {
     System.Xml.XmlDocument updateConfig = new System.Xml.XmlDocument();
     updateConfig.LoadXml(@“〈root〉〈/root〉“);
     updateConfig.Save(desPath + “UpdateConfig.xml“);
    }
    #endregion


    System.Xml.XmlDocument serverXmlDoc = UpdateSvr.AppUpdateVertion();
    System.Xml.XmlDocument localXmlDoc = new System.Xml.XmlDocument();
    localXmlDoc.Load(desPath + “UpdateConfig.xml“);
    bool newVersionExist = false;
    bool moduleExist = false;
    System.Xml.XmlNode serverNode0 = serverXmlDoc.ChildNodes[0];
    System.Xml.XmlNode localNode0 = localXmlDoc.ChildNodes[0];
    foreach(System.Xml.XmlNode serverNode in serverNode0)
    {
     moduleExist = false;
     foreach(System.Xml.XmlNode localNode in localNode0)
     {
      //找到对应模块
      if(localNode.ChildNodes[0].InnerText == serverNode.ChildNodes[0].InnerText)
      {
       moduleExist = true;
       //版本号判断
       if(localNode