ASP.NET网站版本自动更新程序及代码[转]

news/2024/5/8 15:37:27/文章来源:https://blog.csdn.net/as13591884392/article/details/101339379

1、自动更新程序主要负责从服务器中获取相应的更新文件,并且把这些文件下载到本地,替换现有的文件。达到修复Bug,更新功能的目的。用户手工点击更新按钮启动更新程序。已测试。
2、环境VS2008,采用C#.NET和ASP.NET实现。
3、服务器:提供下载文件,发布出去。 文件包括:dll, xml,aspx等格式文件。其中update.xml 是记录更新文件的。
4、客户端:项目里面添加一个autoupdate.xml 文件,该文件里有连接服务器的发布更新文件的服务器地址。当客户端里userupdate.xml文件里的版本号和服务器中update.xml里的版本号对比,如果服务器的版本号高,提醒客户端更新。
5、源代码如下所示。

1)、服务端发布至IIS如下图所示。

 图1

 

其中bin目录下的dll文件属性写入打勾。

图2

Update.xml源码如下所示。

 

代码
< update >
   
< version > 1.0.1.9 </ version >      
   
< datetime > 2009-12-14  </ datetime >        
   
< filelist  filescount ="5"  itemcount ="11"  sourcepath ="http://Localhost/UpdateServ/" >

   
< file  filesname =""   >  
   
< item  name ="xiaxia.txt"  size ="" >  
    
</ item >
   
</ file >
 
  
< file  filesname ="UpFile" >  
    
< item  name ="2222.dll"  size ="" >  
    
</ item >  
    
< item  name ="1162193918505.doc"  size ="" >   
    
</ item >  
    
< item  name ="xd.doc"  size ="" >  
    
</ item >  
    
< item  name ="s2.txt"  size ="" >  
    
</ item >
    
< item  name ="a.dll"  size ="" >  
    
</ item >
    
< item  name ="WebUPFILE.dll"  size ="" >  
    
</ item >
  
</ file >


< file  filesname ="test" >  

< item  name ="aa.txt"  size ="" >   
</ item >  
< item  name ="2.txt"  size ="" >   
</ item >  
</ file >


< file  filesname ="Copy" >


< item  name ="bb.doc"  size ="" >   
</ item >
< item  name ="b.dll"  size ="" >  
</ item >
</ file >  


< file  filesname ="hehe" >

< item  name ="hehe.txt"  size ="" >  
</ item >
< item  name ="WebUPFILE.dll"  size ="" >  
</ item >
</ file >  

</ filelist >  
</ update >  

 

 

2)、客户端代码,结构如下图所示。

图3

代码内有注释,在此不再多说。

Config.cs

 

代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.IO;
using  System.Xml;

namespace  WebUpdate
{
    
public   class  Config
    {
        
public   string  url  =   null ;
        
public   string  cmd  =   null ;

        
// 读文件autoUpdate.xml
         public  Config()
        {
            
string  path  =  AppDomain.CurrentDomain.BaseDirectory  +   " autoUpdate.xml " ;

            
try
            {
                
if  (path  !=   null )
                {
                    XmlDocument xmlDoc 
=   new  XmlDocument();
                    xmlDoc.Load(path);
                    url 
=  xmlDoc.SelectSingleNode( " /update/url " ).InnerText;

                }
            }
            
catch  (Exception ex)
            {
                
throw   new  Exception( " 找不到autoUpdate.xml文件 "   +  ex.Message);

            }
        }

        
// 获取服务器的版本
         public  Version GetServerVersion()
        {
            XmlDocument xmlDoc 
=   new  XmlDocument();
            xmlDoc.Load(url);
            
return   new  Version(xmlDoc.SelectSingleNode( " /update/version " ).InnerText);
        }

        
// 获取客户端版本
         public   string  GetClientVersion()
        {
            url 
=  AppDomain.CurrentDomain.BaseDirectory  +   " userVersion.xml " ;
            XmlDocument xmlUser 
=   new  XmlDataDocument();
            xmlUser.Load(url);
            XmlElement root 
=  xmlUser.DocumentElement;
            XmlNode updateNode 
=  root.SelectSingleNode( " version " );
            
string  version  =  updateNode.Attributes[ " value " ].Value;
            
return  version;

        }

        
// 为了进行版本比较,进行转换为整数比较
         public   int  ConvertVersion( string  value)
        {
            
int  w, z, x, y, temp;
            w 
=   int .Parse(value.Substring( 0 1 ));
            z 
=   int .Parse(value.Substring( 2 1 ));
            x 
=   int .Parse(value.Substring( 4 1 ));
            y 
=   int .Parse(value.Substring( 6 1 ));
            temp 
=  w  *   1000   +  z  *   100   +  x  *   10   +  y;
            
return  temp;
        }

        
// 更新客户版本号为服务器的版本号
         public   string  UpdateVersion( string  serVersion)
        {
            url 
=  AppDomain.CurrentDomain.BaseDirectory  +   " userVersion.xml " ;
            XmlDocument xmlUser 
=   new  XmlDataDocument();
            xmlUser.Load(url);
            XmlElement root 
=  xmlUser.DocumentElement;
            XmlNode updateNode 
=  root.SelectSingleNode( " version " );
            
string  strVer  =  updateNode.Attributes[ " value " ].Value;
            updateNode.Attributes[
" value " ].Value  =  serVersion;
            xmlUser.Save(url);
            
return  serVersion;
        }

    }
}

DownFile.cs

 

代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Net;
using  System.IO;


namespace  WebUpdate
{
    
public   class  DownFile
    {
        
// 从服务器下载文件,若目录存在,直接复制新文件,不存在则新建目录并复制文件,成功后返回1
         public   bool  DownFileFromServ( string  url,  string  fileName)
        {
            
bool  downsucess  =   false ;
            
try
            {
                
string  fileExtraName  =  url.Split( char .Parse( " . " ))[ 0 ];                               // 文件名
                 string  fileAfterName  =  System.IO.Path.GetExtension(url);                 // 文件的扩展名
                 if  (fileAfterName  ==   " .aspx " )
                    url 
=  fileAfterName  +   " .txt " ;
                HttpWebRequest myReq 
=  (HttpWebRequest)WebRequest.Create(url);
                myReq.KeepAlive 
=   true ;
                HttpWebResponse myRes 
=  (HttpWebResponse)myReq.GetResponse();

                downsucess 
=   this .CopyFileAndDirectory(myRes, fileName);

            }
            
catch  (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            
return  downsucess;
        }

        
// 返回复制文件或创建目录是否成功
         public   bool  CopyFileAndDirectory(WebResponse myResp,  string  fileName)
        {
            
bool  flag  =   true ;
            
byte [] buffer  =   new   byte [ 0x400 ];
            
try
            {
                
int  num;
                
// 若本身已有该目录则删除
                 if  (System.IO.File.Exists(fileName))
                {
                    System.IO.File.Delete(fileName);
                }
                
// 创建目录更新到Updat目录下
                 string  directoryName  =  Path.GetDirectoryName(fileName);
                
if  ( ! Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }

                
// 指定文件fileName不存在时创建它
                Stream streamRead  =  System.IO.File.Open(fileName, FileMode.Create);

                Stream responseStream 
=  myResp.GetResponseStream();

                
do
                {
                    
// 从当前读取的字节流数,复制
                    num  =  responseStream.Read(buffer,  0 , buffer.Length);
                    
if  (num  >   0 )
                    {
                        streamRead.Write(buffer, 
0 , num);
                    }
                }
                
while  (num  >   0 );
                streamRead.Close();
                responseStream.Close();
            }
            
catch
            {
                flag 
=   false ;
            }

            
return  flag;

        }

    }
}

 

Update.cs

 

代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Xml;

namespace  WebUpdate
{
    
public   class  Update
    {
        
// 从服务器文件update.xml中获取要下载的文件列表
         public   bool  flag  =   false ;

        
public   string [] GetFileList( string  url)
        {
            XmlDocument xmlDoc 
=   new  XmlDocument();
            xmlDoc.Load(url);
            XmlElement root 
=  xmlDoc.DocumentElement;
            XmlNode updateNode 
=  root.SelectSingleNode( " filelist " );
            
string  soucePath  =  updateNode.Attributes[ " sourcepath " ].Value;

            
string  fileName  =   null ;
            
string  fileList  =   null ;
            
// 取出服务器里update.xml里更新的file文件
            XmlNodeList fileNode  =  updateNode.SelectNodes( " file " );
            
if  (fileNode  !=   null )
            {
                
foreach  (XmlNode i  in  fileNode)
                {
                    
foreach  (XmlNode j  in  i)
                    {
                        
if  (i.Attributes[ " filesname " ].Value  !=   "" )
                            fileName 
=  soucePath  +  i.Attributes[ " filesname " ].Value  +   " / "   +  j.Attributes[ " name " ].Value  +
                                
" $ "   +  i.Attributes[ " filesname " ].Value  +   " / "   +  j.Attributes[ " name " ].Value;
                        
else
                            fileName 
=  soucePath  +  j.Attributes[ " name " ].Value  +
                                
" $ "   +  j.Attributes[ " name " ].Value;

                        fileName 
+=   " , " ;
                        fileList 
+=  fileName;
                    }
                }

                
string [] splitFile  =  fileList.Split( ' , ' );
                flag 
=   true ;
                
return  splitFile;
            }
            
return   null ;
        }

    }
}

 

 

autoUpdate.xml

 

< update >
  
< url > http://Localhost/UpdateServ/update.xml </ url >
</ update >

 

 

userVersion.xml

 

<? xml version="1.0" encoding="utf-8" ?>
< update >
  
< version  value ="1.0.1.9" > 客户端版本号 </ version >
</ update >

 

 

Update.aspx

 

代码
<% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Update.aspx.cs "  Inherits = " _Default "   %>    
  
<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >    
  
< html xmlns = " http://www.w3.org/1999/xhtml " >    
< head runat = " server " >    
    
< title > 自动更新 </ title >    
</ head >    
< body >    
    
< form id = " form1 "  runat = " server " >    
    
< div >    
       
        
< br  />    
        
< br  />    
                                                                          
        
< asp:Label ID = " lblDisplay "  runat = " server "  style = " text-align: center "  mce_style = " text-align: center "     
            Text
= " Label "  Visible = " False " ></ asp:Label >    
        
< br  />    
        
< br  />    
                                                              
        
< asp:Button ID = " btnUpdate "  runat = " server "  style = " text-align: center "  mce_style = " text-align: center "     
            Text
= " 更新 "  onclick = " btnUpdate_Click "  Visible = " False "  Height = " 34px "     
            Width
= " 145px "   />    
        
< br  />    
                              
       
        
< br  />    
        
< asp:Label ID = " NewVersion "  runat = " server "  Text = " Label1 "  Visible = " false " ></ asp:Label >    
        
< br  />    
        
< br  />    
        
< asp:Label ID = " CurrentVersion "  runat = " server "  Text = " Label2 "  Visible = " false " ></ asp:Label >    
       
    
</ div >    
    
</ form >    
</ body >    
</ html >   

 

Update.aspx.cs

 

代码
using  System;
using  System.Configuration;
using  System.Data;
using  System.Linq;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Xml.Linq;
using  WebUpdate;

public   partial   class  _Default : System.Web.UI.Page 
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        lblDisplay.Text 
=   "" ;
        btnUpdate.Visible 
=   false ;


        Config cf 
=   new  Config();

        NewVersion.Text 
=  cf.GetServerVersion().ToString();
        CurrentVersion.Text 
=  cf.GetClientVersion().ToString();

        
int  clientversion  =  cf.ConvertVersion(CurrentVersion.Text);
        
int  serverversion  =  cf.ConvertVersion(NewVersion.Text);
        
if  (serverversion  >  clientversion)
        {
            btnUpdate.Visible 
=   true ;
        }
        
else
        {
            lblDisplay.Text 
=   " 已是最新版本,不需要更新! " ;
            lblDisplay.Visible 
=   true ;
        }

    }
    
protected   void  btnUpdate_Click( object  sender, EventArgs e)
    {
        
string  url  =   null ;
        
string [] files  =   null ;
        Config updatecf 
=   new  Config();
        url 
=  updatecf.url;
        Update upd 
=   new  Update();
        files 
=  upd.GetFileList(url);

        UpdateFile(files);

        CurrentVersion.Text 
=  updatecf.UpdateVersion(NewVersion.Text);

        lblDisplay.Text 
=   " 更新完成。 " ;
        lblDisplay.Visible 
=   true ;
        btnUpdate.Visible 
=   false ;

    }

    
private   void  UpdateFile( string [] files)
    {
        
if  ((files  ==   null ||  (files.Length  <=   0 ))
        {
            Response.Write(
" 升级完成 " );
        }
        
else
        {
            
int  num  =   0 ;
            
for  ( int  i  =   0 ; i  <  files.Length; i ++ )
            {
                
string  str  =  files[i];
                
if  ((str  !=   null &&  (str.Split( new   char [] {  ' $ '  }).Length  ==   2 ))
                {
                    
string [] strArray  =  str.Split( new   char [] {  ' $ '  });
                    
this .UpdateFile(strArray[ 0 ], strArray[ 1 ]);
                    num
++ ;
                }
            }
            
if  (num  ==   0 )
            {
                Response.Write(
" 升级完成 " );
            }
        }
    }

    
private   void  UpdateFile( string  url,  string  filename)
    {
        
string  fileName  =  AppDomain.CurrentDomain.BaseDirectory  +  filename;
        
try
        {
            DownFile file 
=   new  DownFile();
            
bool  flag  =  file.DownFileFromServ(url, fileName);
        }
        
catch
        {

        }
    }
}

转载于:https://www.cnblogs.com/PingleDay/p/3494350.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_868682.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

WebApi托管静态网站(Owin 自托管静态网站)

我们在使用WebApi对外提供简单Api的时候&#xff0c;有时候往往需要同步提供一些简单的静态页面给用户&#xff0c;例如OAuth认证服务&#xff0c;提供一个授权界面等。如果我们单独架设网站&#xff0c;将会导致调用我们自己的接口出现跨域访问&#xff0c;出现IE8及以下浏览器…

那些网站够安全吗?

近日&#xff0c; CSDN 社区网站数据库泄露 &#xff0c;近 600 万用户真实账号密码外泄。该事件横扫整个中文互联网&#xff0c;并且随后又爆出 多玩游戏 800 万用户资料被泄露 &#xff0c;另有传言人人网、开心网、天涯社区、世纪佳缘、百合网等社区都有可能成为黑客下一个目…

5个在线的网站测试和验证工具

网站上线前的测试和验证是非常重要的一个环节&#xff0c;验证的意思是检查网站的页面和其他数据是否符合标准规范&#xff0c;设计规范的网站在各种浏览器上表现会一致而且良好。 为了帮助你执行这些测试和验证&#xff0c;今天我们列表了5个在线的工具。 Pingdom Tools Ping…

使用GitHub建立个人网站

使用GitHub建立个人网站 1 Git简介 2 为什么使用Github Pages 3 创建Github Pages 3.1 安装git工具. 3.2 两种pages模式 3.3 创建步骤 3.4 常用命令 4 使用Jekyll搭建博客 4.1 什么是jekyll 4.2 jekyll本地环境搭建 4.3 jekyll目录结构 4.4 Jekyll-Bootstrap创建博客 4.5 Je…

cnzz统计网站

cnzz是由国际著名风险投资商IDG投资的网络技术服务公司&#xff0c;是中国互联网目前最有影响力 CNZZ网站首页的免费流量统计技术服务提供商&#xff0c;专注于为互联网各类站点提供专业、权威、独立的第三方数据统计分析。同时&#xff0c;CNZZ拥有全球领先的互联网数据采集、…

网站防止攻击

1、什么是XSS XSS又叫CSS (Cross Site Script) &#xff0c;跨站脚本攻击。它指的是恶意攻击者往Web页面里插入恶意html代码&#xff0c;当用户浏览该页之时&#xff0c;嵌入其中Web里面的html代码会被执行&#xff0c;从而达到恶意用户的特殊目的。XSS属于被动式的攻击&#…

如何防止你的网站被攻击?

避免网站被攻击&#xff0c;其实是可以提前预防的&#xff0c;那么要如何预防呢&#xff1f; 1、关闭不必要的端口和服务 2、安装杀毒软件或者是防火墙来抵御攻击。 3、定期修改账户密码&#xff0c;尽量设置的复杂些&#xff0c;不要使用弱密码。 4、日常维护的时候要注意&…

网站前端开发--css篇

Ⅰ 全局&#xff1a;global.css 全局样式为全站公用&#xff0c;为页面样式基础&#xff0c;页面中必须包含。 结构&#xff1a;layout.css 页面结构类型复杂&#xff0c;并且公用类型较多时使用。多用在首页级页面和产品类页面中。 私有&#xff1a;style.css 独立页面所使用的…

scrapy爬取途牛网站旅游数据

描述&#xff1a;采取了scrapy框架对途牛网旅游数据进行了爬取&#xff0c;刚开始练手&#xff0c;所以只爬了四个字段用作测试&#xff0c;分别是景点名称、景点位置、景点开放时间、景点描述&#xff0c;爬取结果存的是json格式。 部分数据&#xff1a; 部分代码&#xf…

网站数据统计分析之一:日志收集原理及其实现

网站数据统计分析工具是网站站长和运营人员经常使用的一种工具&#xff0c;比较常用的有谷歌分析、百度统计 和 腾讯分析等等。所有这些统计分析工具的第一步都是网站访问数据的收集。目前主流的数据收集方式基本都是基于javascript的。本文将简要分析这种数据收集的原理&#…

WordPress建站后必做的几项优化

欢迎大家访问我的个人博客网站&#xff1a;风挽青个人博客 1、修改上传文件大小限制 wordpress上传文件一般都有限制&#xff0c;通常为2M&#xff0c;在一些情况下会不够用&#xff0c;所以自然是提升它的上传文件限制大小。 找到php.ini文件&#xff0c;然后修改其中的几个…

实现基于LNMP的电子商务网站

一、环境准备 yum安装mariadb、 mariadb-mysql、php-fpm、Nginx 并开启服务 二、 1、下载开源的购物商城软件包&#xff0c;解压到/data/test文件夹下&#xff0c;可自定义 unzip 2、修改用户和属组为nobody chown nobody . chgrp -R nobody . 三、 1、修改Nginx配…

SEOmoz开放Open Site Explorer工具API

SEOmoz是SEO行业知名站点&#xff0c;上周SEOmoz推出了外链分析工具Open Site Explorer。今天很高兴告诉大家&#xff0c;Open Site Explorer开始免费开放API了。 英文过得去的可以看原文的介绍&#xff1a;Launching the SEOmoz Free API and Enough Power to Build Open Site…

商机无限!在政府门户网站升级改造中掘金

作者&#xff1a; 王凤霞 2007-10-31 内容导航&#xff1a; 政府网站升级改造高潮已经全... 第1页&#xff1a; 政府网站升级改造高潮已经全面到来 第2页&#xff1a; 政府门户网站升级改造中商机无限 第3页&#xff1a; 政府门户网站升级改造的新特点 第4页&#xff1a; …

超级鹰模拟登录古诗文网站

源码分享: 下面是超级鹰的源码,可以从他们的网站下载,我作了一点修改

网站出现403 Forbidden错误的原因以及怎么解决的方法

这几天刚接手一批新做的网站&#xff0c;在访问网站的时候&#xff0c;会时不时的出现403 Forbidden错误&#xff0c;浏览器会给出403 Forbidden错误提示&#xff0c;在打开Access Error中列出的URL之后, 出现以下错误&#xff1a; 403 Forbidden Access to this resource on t…

说说大型高并发高负载网站的系统架构(更新)

此文系转载&#xff0c;如需转载请保留出处&#xff1a;俊麟 Michael’s blog (http://www.toplee.com/blog/?p71) Trackback Url : http://www.toplee.com/blog/wp-trackback.php?p71 鄙人先后在CERNET做过拨号接入&#xff0c;在Yahoo&3721搞过搜索前端&#xff0c;在…

高并发高流量网站架构

Web2.0 的兴起&#xff0c;掀起了互联网新一轮的网络 创业大潮。以用户为导向的新网站建设概念&#xff0c;细分了网站功能和用户群&#xff0c;不仅成功的造就了一大批新生的网站&#xff0c;也极大的方便了上网的人们。但 Web2.0 以用户为导向的理念&#xff0c;使得新生的网…

一个厂商网站的SQL安全检测 (啊D、明小子)

鄙人今年20&#xff0c;七年前也就是我初一的时候钻研过一段时间的攻防技术&#xff0c;但是由于年纪尚小不懂代码而且以学业为重放弃了继续钻研。 前几天学校一学弟开群拉有兴趣的同学进入&#xff0c;我又想到了过去学到的那些东西&#xff0c;突然又有了感觉&#xff0c;不能…

记录 免费高清视频下载网站

7,000 最精彩的 Videos 图片 100% 免费下载 Pexels 素材图片https://www.pexels.com/zh-cn/search/videos