C#软体自动在线更新代码(一)

//客户端完整代码下载
//服务器空间为.Net 2.0空间
//服务器空间文件列表:
//UpdateSize.ashx
//AutoUpdater   //此文件夹放更新文件
//AutoUpdater/AutoUpdater.xml
UpdateSize.ashx:
<%@ WebHandler Language="C#" Class="UpdateSize" %>
using System; 
using System.Web; 
using System.IO;
public class UpdateSize : IHttpHandler { 
     
    public void ProcessRequest (HttpContext context) { 
        string dirPath = context.Server.MapPath("/AutoUpdater/"); 
        context.Response.ContentType = "text/xml"; 
        context.Response.Expires = -1; 
        context.Response.Write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); 
        context.Response.Write("<UpdateSize Size=\"" + GetUpdateSize(dirPath) + "\" />"); 
        context.Response.End(); 
    }
    /// <summary> 
    /// 获取所有下载文件大小 
    /// </summary> 
    /// <returns>返回值</returns> 
    private static long GetUpdateSize(string dirPath) 
    { 
        //判断文件夹是否存在,不存在则退出 
        if (!Directory.Exists(dirPath)) 
            return 0; 
        long len; 
        len = 0; 
        DirectoryInfo di = new DirectoryInfo(dirPath); 
        //获取所有文件大小 
        foreach (FileInfo fi in di.GetFiles()) 
        { 
            //剔除升级数据文件 
            if (fi.Name != "AutoUpdater.xml") 
                len += fi.Length; 
        } 
        return len; 
    }
    public bool IsReusable { 
        get { 
            return false; 
        } 
    }
}
AutoUpdater.xml:
<?xml version="1.0" encoding="utf-8" ?> 
<AutoUpdater> 
<UpdateInfo> 
    <!--升级文件的更新日期--> 
    <UpdateTime Date = "2008-08-06"/> 
</UpdateInfo> 
<!--升级文件列表--> 
<UpdateFileList> 
    <UpdateFile>Maxthon207.exe</UpdateFile> 
</UpdateFileList> 
<UpdateFileList> 
    <UpdateFile>Maxthon207.exe</UpdateFile> 
</UpdateFileList> 
</AutoUpdater>
客户端
conf.config:
<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
<appSettings> 
    <add key="Url" value="http://localhost/" /> 
    <add key="UpDate" value="2008-08-06" /> 
</appSettings> 
</configuration>
AutoUpdater.cs:
using System; 
using System.ComponentModel; 
using System.Data; 
using System.Globalization; 
using System.IO; 
using System.Net; 
using System.Text; 
using System.Windows.Forms; 
using System.Xml;
namespace Yesuo 
{ 
    public partial class AutoUpdater : Form 
    { 
        private WebClient downWebClient = new WebClient(); 
        private static string dirPath; 
        private static long size;//所有文件大小 
        private static int count;//文件总数 
        private static string[] fileNames; 
        private static int num;//已更新文件数 
        private static long upsize;//已更新文件大小 
        private static string fileName;//当前文件名 
        private static long filesize;//当前文件大小 
         
        public AutoUpdater() 
        { 
            InitializeComponent(); 
        }
        private void ComCirUpdate_Load(object sender, EventArgs e) 
        { 
            dirPath = GetConfigValue("conf.config", "Url"); 
            string thePreUpdateDate = GetTheLastUpdateTime(dirPath); 
            string localUpDate = GetConfigValue("conf.config", "UpDate"); 
            if (!String.IsNullOrEmpty(thePreUpdateDate) && !String.IsNullOrEmpty(localUpDate)) 
            { 
                if (DateTime.Compare( 
                    Convert.ToDateTime(thePreUpdateDate, CultureInfo.InvariantCulture), 
                    Convert.ToDateTime(localUpDate, CultureInfo.InvariantCulture)) > 0) 
                { 
                    UpdaterStart(); 
                } 
                else 
                { 
                    UpdaterClose(); 
                } 
            } 
            else 
            { 
                UpdaterClose(); 
            } 
            //UpdaterStart(); 
        }
        /// <summary> 
        /// 开始更新 
        /// </summary> 
        private void UpdaterStart() 
        { 
            float tempf; 
            //委托下载数据时事件 
            this.downWebClient.DownloadProgressChanged += delegate(object wcsender, DownloadProgressChangedEventArgs ex) 
            { 
                this.label2.Text = String.Format( 
                    CultureInfo.InvariantCulture, 
                    "正在下载:{0} [ {1}/{2} ]", 
                    fileName, 
                    ConvertSize(ex.BytesReceived), 
                    ConvertSize(ex.TotalBytesToReceive));
                filesize = ex.TotalBytesToReceive; 
                tempf = ((float)(upsize + ex.BytesReceived) / size); 
                this.progressBar1.Value = Convert.ToInt32(tempf * 100); 
                this.progressBar2.Value = ex.ProgressPercentage; 
            }; 
            //委托下载完成时事件 
            this.downWebClient.DownloadFileCompleted += delegate(object wcsender, AsyncCompletedEventArgs ex) 
            { 
                if (ex.Error != null) 
                { 
                    MeBox(ex.Error.Message); 
                } 
                else 
                { 
                    if (File.Exists(Application.StartupPath + "\\" + fileName)) 
                    { 
                        File.Delete(Application.StartupPath + "\\" + fileName); 
                    } 
                    File.Move(Application.StartupPath + "\\AutoUpdater\\" + fileName, Application.StartupPath + "\\" + fileName); 
                    upsize += filesize; 
                    if (fileNames.Length > num) 
                    { 
                        DownloadFile(num); 
                    } 
                    else 
                    { 
                        SetConfigValue("conf.config", "UpDate", GetTheLastUpdateTime(dirPath)); 
                        UpdaterClose(); 
                    } 
                } 
            };
            size = GetUpdateSize(dirPath + "UpdateSize.ashx"); 
            if (size == 0) 
                UpdaterClose(); 
            num = 0; 
            upsize = 0; 
            UpdateList(); 
            if (fileNames != null) 
                DownloadFile(0); 
        }
        /// <summary> 
        /// 获取更新文件大小统计 
        /// </summary> 
        /// <param name="filePath">更新文件数据XML</param> 
        /// <returns>返回值</returns> 
        private static long GetUpdateSize(string filePath) 
        { 
            long len; 
            len = 0; 
            try 
            { 
                WebClient wc = new WebClient(); 
                Stream sm = wc.OpenRead(filePath); 
                XmlTextReader xr = new XmlTextReader(sm); 
                while (xr.Read()) 
                { 
                    if (xr.Name == "UpdateSize") 
                    { 
                        len = Convert.ToInt64(xr.GetAttribute("Size"), CultureInfo.InvariantCulture); 
                        break; 
                    } 
                } 
                xr.Close(); 
                sm.Close(); 
            } 
            catch (WebException ex) 
            { 
                MeBox(ex.Message); 
            } 
            return len; 
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值