c#winform自动更新程序小实例,希望对你有帮助

第一步:检查更新
检查更新其实无非就是比较更新包的版本和本地软件版本,如果高,需要更新,否则不需要。下面就是获取版本号的方法。获取软件配置文件。

private bool CheckUpdate()
        {
            bool result = false;
            try
            {
                string Cfg = TxtRead(exePath   "\\Config.txt");
                ConfigLocal = JsonConvert.DeserializeObject<DTO_Config>(Cfg);
 
                CheckUpdateURL = ConfigLocal.AutoUpdateURL;
 
                Cfg = TxtRead(CheckUpdateURL   "\\Config.txt");
                ConfigRemote = JsonConvert.DeserializeObject<DTO_Config>(Cfg);                
 
                VersionR = ConfigRemote.Version;
                VersionL = ConfigLocal.Version;
                int VersionRemote = int.Parse(ConfigRemote.Version.Replace(".", ""));
                int VersionLocal = int.Parse(ConfigLocal.Version.Replace(".", ""));
 
                result = VersionRemote > VersionLocal;
            }
            catch { }
            return result;
        }

第二步:下载更新包
下载更新包就是去访问服务端文件,然后复制下来或者下载下来。下面是访问网络和访问局域网。
1.访问远程网络地址,使用webclient

public void DownLoadFile()
        {
            if (!Directory.Exists(UpdateFiles))
            {
                Directory.CreateDirectory(UpdateFiles);
            }
            using (WebClient webClient = new WebClient())
            {
                try
                {
                    webClient.DownloadFileCompleted  = new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                    webClient.DownloadProgressChanged  = new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                    webClient.DownloadFileAsync(new Uri(CheckUpdateURL   "\\UpdateFile.rar"), UpdateFiles   "\\UpdateFile.rar");
                }
                catch (WebException ex)
                {
                    MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

downloadprogresschanged:监听异步下载进度

private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            this.progressBarUpdate.Minimum = 0;
            this.progressBarUpdate.Maximum = (int)e.TotalBytesToReceive;
            this.progressBarUpdate.Value = (int)e.BytesReceived;
            this.lblPercent.Text = e.ProgressPercentage   "%";
        }

downloadfilecompleted:监听完成异步文件下载

private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                this.lblMessage.Text = "下载完成";
                //复制更新文件替换旧文件
                DirectoryInfo TheFolder = new DirectoryInfo(UpdateFiles);
                foreach (FileInfo NextFile in TheFolder.GetFiles())
                {
                    File.Copy(NextFile.FullName, Application.StartupPath   NextFile.Name, true);
                }
                
            }
        }

2.访问服务端公共网盘,采用file.copy

public void GetRemoteFile()
        {
            try
            {
                DirectoryInfo TheFolder = new DirectoryInfo(CheckUpdateURL);
                FileInfo[] FileList = TheFolder.GetFiles();
                this.progressBarUpdate.Minimum = 0;
                this.progressBarUpdate.Maximum = FileList.Length;
 
                foreach (FileInfo NextFile in FileList)
                {
                    if (NextFile.Name != "Config.txt")
                    {
                        File.Copy(NextFile.FullName, exePath   "\\"   NextFile.Name, true);                        
                    }
                    this.lblMessage.Text = "更新"   NextFile.Name;
                    this.progressBarUpdate.Value  = 1;
                    this.lblPercent.Text = "更新进度... "   (this.progressBarUpdate.Value / FileList.Length) * 100   "%";
                }
                this.lblMessage.Text = "更新完成";
                //更改本地版本号为最新版本号
                ConfigLocal.Version = VersionR;
                string cfgs = JsonConvert.SerializeObject(ConfigLocal);
                TxtWrite(Application.StartupPath   "\\Config.txt", cfgs);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、软件开发环境以及开发工具: 框架:.NET Framework 4.0 工具:Visual Studio 2017 插件:DevExpress 18.1.7 环境:IIS 7 二、实现步骤 (1)在项目中创建一个名为WinformAutoUpdate.APP的Winform窗体应用工程,并将默认的Form1.cs窗体文件重命名为MainFrm.cs作为主程序窗体 创建主程序窗体 (2)在项目中再创建一个名为AutoUpdateTask的Winform应用程序工程,并将默认的Form1.cs窗体文件重命名为AutoUpdateTaskFrm.cs作为更新程序窗体 创建更新程序窗体 (3)在更新程序窗体中放入图上所示的相关控件; 进度条控件用于显示更新进度,放入一个Button按钮控件用于用户根据提示进行操作! 实现思路: 1、将更新程序放入磁盘的目录下面,并将其放在已经发布了的IIS中 当用户在运行主程序窗体并点击左上角的更新按钮时,弹出程序更新窗体,并先自动从IIS中拉取updateList.xml文件,然后与本地程序作对比,检测是否需要升级软件; 如果有新版本发布,则点击“立即更新”按钮,程序将从IIS中拉取新发布的ZIP软件包,并自动解压到主程序目录中,覆盖主程序目录中的相关文件(这里值得注意的是,在解压程序之前,我们需要先结束主程序的进程,不然会因主程序进程正在使用而导致报错,另外,我们用到的插件是ICSharpCode.SharpZipLib.dll第三方动态链接库,网上有现成的,可以直接Down下来用);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值