C# C/S架构软件的自动更新功能(http方式) 代码1

这是一个C#实现的C/S架构软件自动更新功能,通过HTTP方式从服务器下载更新文件。代码中使用WebClient类进行文件下载,首先获取服务器版本信息并与本地版本进行比较,若有新版本则下载更新文件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using System.Net;
using System.IO;
using System.Threading;

using System.Xml;
using System.Data.SqlClient;

namespace getfile
{
      {
        private WebClient client = new WebClient();

        string serviceversion;
        string localversion;
        string url;  //服务器url
        string urlpath;
        string path;
        string localpath;
        string serverpath;
        string connectionstring;
        string messagelanguage;
        bool bl;      //验证是否连接服务器,true连接,false不连接
        string number;//需要更新的文件个数
        int i;//记录有多少个文件被下载
        string exe;//更新程序要调用的可执行程序
        string strr;//下载文件的路径

        public FrmUpdate()
        {
            InitializeComponent();          

        }           
       
        private void StartDownload()
        {
            //btnstart.Enabled = false;
            string URL = "http://localhost/update/version.xml";
            int n = URL.LastIndexOf('/');
            string URLAddress = URL.Substring(0, n);
            string fileName = URL.Substring(n + 1, URL.Length - n - 1);
            string Dir = Application.StartupPath;
            string Path = Dir + '\\' + fileName;

            try
            {
                WebRequest myre = WebRequest.Create(URLAddress);
            }
            catch (WebException exp)
            {
                MessageBox.Show(exp.Message, "Error");
            }

            try
            {
                //statusBar.Text = "开始下载文件...";
                //client.DownloadFile(URLAddress, fileName);
                //Stream str = client.OpenRead(URLAddress);
                Stream str = client.OpenRead(URL);
                               
                StreamReader reader = new StreamReader(str);
                byte[] mbyte = new byte[100000];
                int allmybyte = (int)mbyte.Length;
                int startmbyte = 0;
                //statusBar.Text = "正在接收数据...";
                while (allmybyte > 0)
                {
                    int m = str.Read(mbyte, startmbyte, allmybyte);
                   
                    if (m == 0)
                        break;

                    startmbyte += m;
                    allmybyte -= m;
                }

                FileStream fstr = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write);
                fstr.Write(mbyte, 0, startmbyte);
                str.Close();
                fstr.Close();

                //statusBar.Text = "下载完毕!";
            }
            catch (WebException exp)
            {
                MessageBox.Show(exp.Message, "Error");
                //statusBar.Text = "";
            }

            //btnstart.Enabled = true;
        }
       
        private void btnstart_Click(object sender, EventArgs e)
        {
            LblStatus.Visible = false;

            Thread th1 = new Thread(new ThreadStart(ValidateConnect));
            th1.Start();

            PgbConnect.Minimum = 0;
            PgbConnect.Maximum = 100;
            timer1.Enabled = true;

            //if (bl == true)
            //{
            //    //先把服务器端的配置文件下下来
            //    string localstr = Application.StartupPath + @"\updatelocalconfig.xml";

            //    string tempurlpath;
            //    this.ReadXML(out url, localstr, "service"); //读取本地配置文件的服务器路径
            //    tempurlpath = url + @"/updateserverconfig.xml";

            //    string str = Application.StartupPath + @"\updateserverconfig.xml";
            //    WebClient wc = new WebClient();
            //    wc.DownloadFile(tempurlpath, str);  //下载服务器配置文件

            //    this.ReadXML(out number, str, "number");

            //    Thread th = new Thread(new ThreadStart(downloadconfigfile));
            //    th.Start();

            //    this.loadFiles();
            //}

            //try
            //{
            //    string localstr = Application.StartupPath + @"\updatelocalconfig.xml";
            //    this.ReadXML(out url, localstr, "service"); //读取本地配置文件的服务器路径

            //    WebRequest myre = WebRequest.Create(url);  //验证是否可以连接服务器              
            //    myre.Timeout = 3000;//设置3秒超时

            //    Thread th1 = new Thread(new ThreadStart(ValidateConnect));
            //    th1.Start();               
            //    myre.GetResponse();//请求响应

            //    Thread th = new Thread(new ThreadStart(downloadconfigfile));
            //    th.Start();
            //}
            //catch (WebException exp)
            //{
            //    label2.Text = "连接不上网络";
            //    progressBar1.Visible = false;
            //    label2.Visible = true;

            //    MessageBox.Show(exp.Message, "Error");
            //}

            ////Thread th = new Thread(new ThreadStart(downloadconfigfile));
            ////th.Start();           
        }        

        private void downloadconfigfile()
        {
            string localstr = Application.StartupPath + @"\updatelocalconfig.xml";

            string tempurlpath;
            this.ReadXML(out url, localstr, "service"); //读取本地配置文件的服务器路径
            tempurlpath = url + @"/updateserverconfig.xml";


            string str = Application.StartupPath + @"\updateserverconfig.xml";
            WebClient wc = new WebClient();
            wc.DownloadFile(tempurlpath, str);  //下载服务器配置文件


            this.ReadXML(out serviceversion, str, "version");  //读取下载下来的服务器配置文件的版本号

            this.ReadXML(out localversion, localstr, "version");  //读取本地配置文件的版本号

            this.ReadXML(out number, str, "number");

            if (String.Compare(serviceversion, localversion, StringComparison.InvariantCulture) == 1)  //比较两个版本号,如果有新版本,下载新版本
            {
                //this.GetConnectionString(Application.StartupPath+@"\Config\DBConfig.xml");
                //this.GetMessageLanguage(Application.StartupPath+@"\Config\SYSConfig.xml");

              
                DialogResult result = MessageBox.Show(this.GetMessage("UpdateVersionHave") + serviceversion + this.GetMessage("UpdateVersionDownload"), this.GetMessage("UpdateVersionUpdate"), MessageBoxButtons.OKCancel);

                if (result == DialogResult.OK)
                {
                    //在这儿下载
                    this.checkxml("update/updatelist");

                    this.copy("update/updatelist");

                    Directory.Delete(@"c:\temp", true);

                    this.UpdateXmlVersion(localstr);  //下载完后更新本地配置文件版本号
                }

            }
            else
            {
                //MessageBox.Show("没有新版本下载");
            }

            //调用主程序           
            this.ReadXML(out exe, localstr, "exename");

            System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
            info.FileName = exe;
            info.WorkingDirectory = Application.StartupPath;

            System.Diagnostics.Process aa;
            aa = System.Diagnostics.Process.Start(info);


            //this.close();
            this.Invoke(new MyInvoke(closeme));

        }//下载配置文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值