关于bcb升级程序

先前不是搞c++开发的结果公司让我用bcb开发个升级程序费了我好大劲,终于功夫不负有心人现在贴出来大家分享一下希望对大家有点帮助:

我是这样处理的我用nmftp工具在属性中指定用户名,密码,端口 

这里代码是放在要目录下update.exe 文件如果主程序检测到有新版本就会启动update.exe文件来更新程序

我这里用到了批处理主要是来更新数据库,是直接调用你要更新的sql脚本的,还有备份安全设置就是我在更新程序的时候先把一些要更新的文件备份到temp文件夹下,如果更新中间可能有你要取消就会把temp文件夹中内容再更新过来

//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <Inifiles.hpp>
#include <NMFtp.hpp>
#include <Psock.hpp>
#include "SUIForm.hpp"
#include <ExtCtrls.hpp>
#include "SUIButton.hpp"
#include "SUIProgressBar.hpp"
#include <Graphics.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published: // IDE-managed Components
        TNMFTP *NMFTP1;
        TsuiForm *suiForm1;
        TsuiButton *Button1;
        TsuiButton *suiButton1;
        TLabel *Label2;
        TLabel *Label3;
        TProgressBar *ProgressBar1;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall NMFTP1PacketRecvd(TObject *Sender);
        void __fastcall suiButton1Click(TObject *Sender);
private: // User declarations
bool Checkcmd();
void UpdateConfig();
void __fastcall MyOnMessage(tagMSG &Msg, bool &Handled);
public:  // User declarations
        __fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif

下面是cpp文件

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SUIForm"
#pragma link "SUIButton"
#pragma link "SUIProgressBar"
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
        : TForm(Owner)
{
 // SendMessage(this->suiButton1->Handle,245,0,0)  ;
  //Application->OnMessage=MyOnMessage;
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
    this->NMFTP1->Connect();
    if(this->NMFTP1->Connected)
{         bool flag;
          this->Button1->Visible=false;
          AnsiString asBackupDir=ExtractFileDir(Application->ExeName)+"//temp"  ;
          if(!DirectoryExists(asBackupDir))                                  //创建备份目录
          {
                  if   (!CreateDir(asBackupDir))
                  {
                          Application->MessageBox(("无法创建备份目录:"+asBackupDir).c_str(),"错误!");
                          return;
                  }
          }
          AnsiString     asDestFile   =   asBackupDir+"//备份文件的名字";                         //备份文件的名字
          AnsiString str=ExtractFileDir(Application->ExeName) +"//当前应用程序名";
          if(CopyFile(str.c_str(),asDestFile.c_str(),FALSE)!=0)                               //把一个文件移动一个文件夹下
          {
               asDestFile   =   asBackupDir+"//批处理文件";
               str=ExtractFileDir(Application->ExeName) +"//批处理文件";
               CopyFile(str.c_str(),asDestFile.c_str(),FALSE)  ;
               asDestFile   =   asBackupDir+"//sql脚本";
               str=ExtractFileDir(Application->ExeName) +"//sql脚本";
               CopyFile(str.c_str(),asDestFile.c_str(),FALSE)   ;
               this->suiButton1->Visible=true;        //这里显示取消操作
              if(this->Checkcmd())
              {
                NMFTP1->Download("htdocs//tmp//Updateds.sql",GetCurrentDir()+"//sql脚本");
                NMFTP1->Download("htdocs//tmp//beauty.cmd",GetCurrentDir()+"//批处理文件");
                WinExec("批处理文件",0);     //现在是执行批处理
              }
              NMFTP1->Download("htdocs//tmp//beauty.exe",GetCurrentDir()+"//程序名");
              this->UpdateConfig();
          }
}
   this->Label2->Visible=true;
   this->Label2->Caption="更新完毕,请稍候";
 
    this->NMFTP1->Disconnect();   //断开连接
   Application->Terminate();
   HANDLE     hd1;
   ShellExecute(hd1,"open","程序名",0,0,SW_SHOW);                                      //启动更新程序
   hd1=NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::NMFTP1PacketRecvd(TObject *Sender)
{
       ProgressBar1->Min = 0;                // 最小值
       ProgressBar1->Max = NMFTP1->BytesTotal;      // 最大值
       ProgressBar1->Step = 1;             // 步长
       for(int i=1; i<NMFTP1->BytesTotal; i+=ProgressBar1->Step ) // 累加进度条
       {
       ProgressBar1->StepIt();
       }
       this->Label3->Caption = IntToStr(NMFTP1->BytesRecvd)+" bytes of "+IntToStr(NMFTP1->BytesTotal)+" received";
}
//---------------------------------------------------------------------------
 bool TForm2::Checkcmd()
 {
     int OldVersion,NewVersion;
     TIniFile  *fold;
     fold=new TIniFile(GetCurrentDir()+"//配置文件");
     OldVersion=fold->ReadInteger("config","fileversion",0);
     fold->Free();
     TIniFile *fnew;
     fnew=new TIniFile(GetCurrentDir()+ "//下载的配置文件");
     NewVersion=fnew->ReadInteger("config","fileversion",0);
     if(NewVersion>OldVersion)
     {
        return true;
     }
     else
     return false;
 }
 void TForm2::UpdateConfig()
 {
     DeleteFile(GetCurrentDir()   +   "//配置文件");                      //DELETE UPDATEFILE
     RenameFile(GetCurrentDir()   +   "//配置文件",GetCurrentDir()   +   "//配置文件");
 }

//取消处理方法
void __fastcall TForm2::suiButton1Click(TObject *Sender)
{
   if(Application->MessageBoxA("确实要取消升级操作吗?","提示信息",MB_YESNO)==IDYES)
   {
          this->NMFTP1->Disconnect();
          this->NMFTP1->Abort();
          AnsiString asBackupDir=ExtractFileDir(Application->ExeName)+"//temp"  ;
          AnsiString     asDestFile   =   asBackupDir+"//备份文件的名字";                         //备份文件的名字
          AnsiString str=ExtractFileDir(Application->ExeName) +"//备份文件的名字";
          if(CopyFile(asDestFile.c_str(),str.c_str(),FALSE)!=0)
          {
               asDestFile   =   asBackupDir+"//批处理文件";
               str=ExtractFileDir(Application->ExeName) +"//批处理文件";
               CopyFile(asDestFile.c_str(),str.c_str(),FALSE)  ;
               asDestFile   =   asBackupDir+"//sql脚本";
               str=ExtractFileDir(Application->ExeName) +"//sql脚本 文件";
               CopyFile(asDestFile.c_str(),str.c_str(),FALSE)   ;
               this->suiButton1->Visible=true;                                                      //这里显示取消操作
               HANDLE     hd1;
             ShellExecute(hd1,"open","应用程序名",0,0,SW_SHOW);                                      //启动更新程序
             hd1=NULL;
          }
          else
          ShowMessage("系统正忙,请与管理员联系");
      }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值