当前位置: 移动技术网 > IT编程>开发语言>c# > C#基于FTP协议的简易软件自动升级程序

C#基于FTP协议的简易软件自动升级程序

2019年07月18日  | 移动技术网IT编程  | 我要评论

最近在重写了一个老的产品条码扫描程序,客户端数越有30个,因为经常有更新,c/s维护非常不方便,所以做一个自动更新程序特别有必要。

在网上随便找了找自动更新的方案,大多使用vs的发布/更新功能,不太喜欢此方案,觉得太麻烦,然后自己初步琢磨了一下如下方案。

现在比较喜欢的编程方式,先画图。

软件更新流程

安装包的制作

为了简单,采用了自解压压缩包的方式制作更新包,将整个新程序包打包为一个自解压压缩包。

以下是我的自解压包的参数设置,怎样制作自解压包网上有很多教程,可以自己去搜索。

常规-解压路径:"c:\barcodesys"
设置-解压后运行:"c:\barcodesys\updatelog.txt"
模式:隐藏启动对话框。
高级:添加快捷方式到桌面
源文件:"c:\barcodesys\barcodemain.exe"
快捷方式名:条码扫描程序
更新:解压替换、覆盖所有。

制作更新程序

因为软件在更新时update程序是一直在运行的,这样在自解压包解压时就会无法替换更新程序,所以将update程序作为一个单独的窗体程序来做,由主程序启动时调用。

制作更新程序前,首先解决怎样判断是否存在最新版本的问题,我采用ftp服务器上新建一个文本文档名称为svrver.txt,内容为最新版本号,本地程序文件夹中建立一个文本文档名称为version,内容为本地程序版本号。方法检查服务器版本号是否与本地版本一致,如果一致则表示不需要更新,否则相反,如下图

以下为更新程序下载文件部分的代码

long filesize = helper.getfilesize(dir+"/release.exe");  //获得更新程序文件大小
        filesizes = filesize;
        if (filesize > 0)
        {
          updatelabeldelegate("发现最新版本"+ server + "!文件大小:" + filesize+"字节"); //更新界面label控件的委托
          int readsize = 0;
          bool result = false;
          using (filestream fs = new filestream("release.exe", filemode.create))
          {
            string url = "ftp://" + ftpaddress + "/"+dir+"/release.exe";
            console.writeline(url);
            ftpwebrequest request = (ftpwebrequest)ftpwebrequest.create(new uri(url));
            request.credentials = new networkcredential(ftpusername, ftppassword);
            request.method = webrequestmethods.ftp.downloadfile; ;
            console.writeline(request);
            request.contentoffset = fs.length;
            updatelabeldelegate("正在下载新版本... 0字节/" + filesize + "字节");  
            using (ftpwebresponse response = (ftpwebresponse)request.getresponse())
            {
              fs.position = fs.length;
              byte[] buffer = new byte[1024*2];
              int count = response.getresponsestream().read(buffer, 0, buffer.length);
              while (count > 0)
              {
                fs.write(buffer, 0, count);
                count = response.getresponsestream().read(buffer, 0, buffer.length);
                readsize += count;
                double pa = (((double)readsize / (double)filesizes) * 100);
                updateprogdelegate((int)pa) ; //更新进度条的委托
                updateuilabel("正在下载更新..."+ (readsize) + "字节/" + (filesize) + "字节");
                application.doevents();
              }
              updateuilabel("下载完成,正在安装新版本,请稍等..." + (filesize) + "字节/" + (filesize) + "字节");
              updateprogdelegate(100);
              response.getresponsestream().close();
            }
          }
          process p = process.start("release.exe");  //下载完成后调用自解压包
          p.waitforexit();  //等待自解压包解压完成


      p.startinfo.filename = "barcodemain.exe";//启动主程序
            p.startinfo.arguments = "update";//传递主启动程序的参数,以免启动主程序后再次启动更新程序
            p.start();//启动 
   this.close();    //关闭更新程序窗口

主程序的部分小修改

因为每次启动时主程序时会调用update程序,同时关闭自身进程以准备更新,所以需要在主程序里做一些小事情,否则陷入启动主程序-启动更新程序-启动主程序的就无限循环中了。

1、修改主程序入口main方法,增加参数

[stathread]
static void main(string[] arg) //接受参数
{
  application.enablevisualstyles();
  application.setcompatibletextrenderingdefault(false);
  application.run(new index(arg)); //传递给主程序窗体的构造函数
}

2、主程序增加带参数的构造函数

private string[] targ ; //接收的参数
public index()
{
  initializecomponent();
}
public index(string[] arg) //带参数的构造函数
{
  initializecomponent();
  targ = arg;
}

3、判断是否是否带参数启动

//检查执行参数     
private static system.diagnostics.process p;
if (targ.length==0)  //如果没有传递参数,则调用update程序
 {
    if (p == null)
   {
     p = new system.diagnostics.process();
      p.startinfo.filename = "updateprg.exe";
      p.start();
      application.exit();
        }
        else
        {
          if (p.hasexited) //是否正在运行
        {
        p.start();
       }
     }
  p.startinfo.windowstyle = system.diagnostics.processwindowstyle.normal;
}

最终的运行效果

最后推荐一个绿色版比较好用的简易ftp软件quick easy ftp server,足以满足更新所用。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网