| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付赛孚耐:软件保护加密专家
身份认证令牌USB KEY   
共有 328 人关注过本帖
标题:webservice实现自动更新
收藏  订阅  推荐  打印 
reandy
Rank: 1
等级:新手上路
帖子:21
积分:310
注册:2007-10-15
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;
        }



     [bold]WebServices的GetUpdateData方法。[/bold]
        [WebMethod(Description="在线更新软件")]        [SoapHeader("sHeader")]        public System.Xml.XmlDocument GetUpdateData()        {            //验证用户是否登陆            if(sHeader==null)                return null;            if(!DataProvider.GetInstance.CheckLogin(sHeader.Username,sHeader.Password))                return null;            //取得更新的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;        }    [bold]在客户端进行的工作。[/bold]
      首先引用此WebServices,例如命名为:WebSvs,
     string nVer = Start.GetService.GetVer(); 
     if(Application.ProductVersion.CompareTo(nVer)<=0)
      update();

  在本代码中 Start.GetService是WebSvs的一个Static 实例。首先检查版本,将结果与当前版本进行比较,
如果为新版本则执行UpDate方法。

        void update()        {            this.statusBarPanel1.Text = "正在下载...";            System.Xml.XmlDocument doc = ((System.Xml.XmlDocument)Start.GetService.GetUpdateData());            doc.Save(Application.StartupPath + @"\update.xml");            System.Diagnostics.Process.Start(Application.StartupPath + @"\update.exe");            Close();            Application.Exit();        }
这里为了简单起见,没有使用异步方法,当然使用异步方法能更好的提高客户体验,这个需要读者们自己去添加。
:) update的作用是将升级的XML文件下载下来,保存为执行文件目录下的一个Update.xml文件。任务完成,
退出程序,等待Update.Exe 来进行升级。     [bold]Update.Exe 的内容。[/bold]         private void Form1_Load(object sender, System.EventArgs e)        {            System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();            foreach(System.Diagnostics.Process p in ps)            {                //MessageBox.Show(p.ProcessName);                if(p.ProcessName.ToLower()=="customerapplication")                {                    p.Kill();                    break;                }            }            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();                //File.Delete(Application.StartupPath + @"\" + fileName);                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("CustomerApplication.exe");            Close();            Application.Exit();        }
搜索更多相关主题的帖子: webservice  自动  doc  XmlDocument  root  
2007-11-25 20:55
FenteLi
Rank: 2
来自:上海
等级:注册会员
帖子:124
积分:1382
注册:2007-11-24

我晕,好乱阿。怎么看啊。至少应该编辑一下再发啊。
2007-11-28 12:41
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.074182 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved