【转载】C# 自动更新程序

http://blog.csdn.net/gisfarmer/article/details/4437994#comments

现在但凡是一个程序都有相应的升级程序,如果你的程序没有相应的升级程序,那么你就需要留意了。你的用户很可能丢失!!!网上关于自动升级的例子也有很多,恰好今天CSDN上的一位老友也询问这件事情,所以就把代码共享大家了。

先个几个图:

 

 

主要原理(相当简单):

升级程序一定要是一个单独的exe,最好不要和你的程序绑到一起(否则升级程序无法启动)。主程序退出----升级程序启动----升级程序访问你的网站的升级配置文件-----读取配置文件里面信息-----下载-----升级程序关闭----主程序启动

主要代码:

1.读取配置文件:

        private void GetUpdateInfo()
        {
            //获取服务器信息
            WebClient client = new WebClient();
            doc = new XmlDocument();
            try
            {
                doc.Load(client.OpenRead("http://192.168.3.43/update/update.xml"));
                //doc.Load(client.OpenRead(Config.IniReadValue("Update","UpdateURL",Application.StartupPath+"//config.ini")+"//update.xml"));
                //doc.Load(Application.StartupPath + "//update.xml");
                client = null;
            }
            catch
            {
                this.labHtml.Text = "无法取得更新文件!程序升级失败!";
                return;
            }
            if (doc == null)
                return;
            //分析文件
            XmlNode node;
            //获取文件列表
            string RootPath = doc.SelectSingleNode("Product/FileRootPath").InnerText.Trim();
            node = doc.SelectSingleNode("Product/FileList");
            if (node != null)
            {
                foreach (XmlNode xn in node.ChildNodes)
                {
                    this.listView1.Items.Add(new ListViewItem(new string[]
                        {
                             xn.Attributes["Name"].Value.ToString(),                            
                             new WebFileInfo(RootPath+xn.Attributes["Name"].Value.ToString()).GetFileSize().ToString(),
                            "---"
                        }));
                }
            }
        }

2.文件下载:

/// <summary>
/// 下载文件
/// </summary>
public void Download()
{
FileStream fs = new FileStream( this.strFile,FileMode.Create,FileAccess.Write,FileShare.ReadWrite );
try
{
this.objWebRequest = (HttpWebRequest)WebRequest.Create( this.strUrl );
this.objWebRequest.AllowAutoRedirect = true;
// int nOffset = 0;
long nCount = 0;
byte[] buffer = new byte[ 4096 ]; //4KB
int nRecv = 0; //接收到的字节数
this.objWebResponse = (HttpWebResponse)this.objWebRequest.GetResponse();
Stream recvStream = this.objWebResponse.GetResponseStream();
long nMaxLength = (int)this.objWebResponse.ContentLength;
if( this.bCheckFileSize && nMaxLength != this.nFileSize )
{
throw new Exception( string.Format( "文件/"{0}/"被损坏,无法下载!",Path.GetFileName( this.strFile ) ) );
}
if( this.DownloadFileStart != null )
this.DownloadFileStart( new DownloadFileStartEventArgs( (int)nMaxLength ) );
while( true )
{
nRecv = recvStream.Read( buffer,0,buffer.Length );
if( nRecv == 0 )
break;
fs.Write( buffer,0,nRecv );
nCount += nRecv;
//引发下载块完成事件
if( this.DownloadFileBlock != null )
this.DownloadFileBlock( new DownloadFileEventArgs( (int)nMaxLength,(int)nCount ) );
}
recvStream.Close();
//引发下载完成事件
if( this.DownloadFileComplete != null )
this.DownloadFileComplete( this,EventArgs.Empty );
}
finally
{
fs.Close();
}
}

欢迎转载,转载请注明出处:http://blog.csdn.net/gisfarmer/article/details/4437994#comments

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值