[转]自己写的一个在线升级的程序!

void CUpdateDlg::OnButtonDoupdate()
{

         // 读取升级文件
          CFileStatus rStatus;
          CString szIniData;
          CString inFile;
          CString tmp;


          inFile=m_szPath+"//update.ini";

          CFile UpdateFile;
          UpdateFile.Open( inFile, CFile::modeRead);
          UpdateFile.GetStatus(inFile,rStatus);
          UpdateFile.Read(szIniData.GetBuffer(rStatus.m_size),rStatus.m_size);
          UpdateFile.Close();

          CStringArray arType;
          CStringArray arVersion;
          for(int i=1;i<=27;i+=2){
                    AfxExtractSubString(tmp,szIniData,i,'/n');
                     tmp.TrimRight();
                     tmp.TrimRight();
                    arType.Add(tmp);
          }
          for(int j=2;j<=28;j+=2){
                    AfxExtractSubString(tmp,szIniData,j,'/n');
                    tmp.TrimRight();
                    tmp.TrimRight();
                    arVersion.Add(tmp);
          }

          GetDlgItem(IDC_BUTTON_DOUPDATE)->EnableWindow(FALSE);

          CProgressCtrl* pBar = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS_UPDATE);
           pBar->SetPos(5);
          m_szEditMessage = "/r/n取得网络升级地址/r/n"+ServerUrl;
          UpdateData(FALSE);

          CInternetSession mySession(NULL,0);
          CHttpFile* myHttpFile=NULL;
          BOOL bIsNewer = FALSE;
          CString myData;
          CString myUrl;
          CString m_SiteRec;
          myUrl = ServerUrl+"?pro=update";
          myHttpFile=(CHttpFile*)mySession.OpenURL(myUrl);
          while(myHttpFile->ReadString(myData)){m_SiteRec = myData;}
          myHttpFile->Close();
          mySession.Close();


          if(m_SiteRec=="ok"){
                    pBar->SetPos(10);
                    m_szEditMessage = "/r/n已经连通....../r/n"+m_szEditMessage;
                    UpdateData(FALSE);


                    for(int k=0;k<14;k++){
                              CInternetSession mySession(NULL,0);
                              CHttpFile* myHttpFile=NULL;
                              m_SiteRec.Empty();
                              myUrl = ServerUrl+"?pro=update&module="+arType[k]+"&version="+arVersion[k];
                              myHttpFile=(CHttpFile*)mySession.OpenURL(myUrl);
                              while(myHttpFile->ReadString(myData)){m_SiteRec += myData;}
                               if(m_SiteRec != "ok"){
                                         CString  inFile;
                                         inFile=m_szPath+"//data//"+arType[k];
                                         CFile NetFile;
                                          NetFile.Open( inFile, CFile::modeCreate | CFile::modeWrite);
                                          NetFile.Write( m_SiteRec, m_SiteRec.GetLength());
                                          NetFile.Close();
                                          bIsNewer = TRUE;
                              }
                              pBar->SetPos((k+1)*(100-10)+10);
                              m_szEditMessage = "/r/n检查"+arType[k]+"文件...  成功!/r/n"+m_szEditMessage;
                              UpdateData(FALSE);
                              myHttpFile->Close();
                              mySession.Close();
                    }


                  if(bIsNewer){
                     ::MessageBox(NULL,"文件已经更新成功!",NULL,MB_ICONASTERISK);
                    }else{
                              ::MessageBox(NULL,"您的数据文件已经是最新版本了!",NULL,MB_ICONASTERISK);
                    }
          }else{
                    ::MessageBox(NULL,"没有连接网络或者找不到升级服务器,请检查",NULL,MB_ICONASTERISK);
                    pBar->SetPos(100);
          }

 // 下面是完成了.
          GetDlgItem(IDCANCEL)->ShowWindow(SW_HIDE);
          GetDlgItem(IDOK)->ShowWindow(SW_SHOW);
          GetDlgItem(IDOK)->SetFocus();
}

你可能感兴趣的:([转]自己写的一个在线升级的程序!)