先前不是搞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("系統正忙,請與管理者聯系");
}
}