最简单的在VC中用MFC中FTP下载功能的实现

(1)、确定一个FTP服务器,这里假设本地主机。

(2)、新建一个基于对话框的应用程序,在对话框的源文件中引用“afxinet.h”源文件。

(3)、在对话框的OnInitDialog方法中链接指定的FTP服务器,将FTP根目录下的文件显示出来(没有显示文件夹)。

 CInternetSession* pSession;
 pSession = new CInternetSession;  //构造新的连接
 CFtpConnection* pFtpCon;
 pFtpCon = pSession->GetFtpConnection("192.168.135.110","cheng",NULL,21);//连接FTP服务器

 CFtpFileFind ftpfind(pFtpCon);
 if (ftpfind.FindFile(NULL)) //查找所有的文件
 {
  CString str ;
  while ( ftpfind.FindNextFile())
  {
   if (!ftpfind.IsDirectory()) //判断是否是目录
   {
    str = ftpfind.GetFileName();
    m_list.AddString(str);
   }
  }
  if (!ftpfind.IsDirectory())
  {
   str = ftpfind.GetFileName();
   m_list.AddString(str);
  }
 }
 
 delete pSession;

(4)、下载FTP服务器上的指定文件,代码为:

void CFTPDownLoadDlg::OnButton1()
{
 // TODO: Add your control notification handler code here
 CInternetSession *pSession;
 pSession=new CInternetSession;
 CFtpConnection *pFtpCon;
 pFtpCon=pSession->GetFtpConnection("192.168.135.110","cheng",NULL,21);
 CString selfile;
 m_list.GetText(m_list.GetCurSel(),selfile);
 if(!selfile.IsEmpty())pFtpCon->GetFile(selfile,"c://"+selfile);
}

你可能感兴趣的:(最简单的在VC中用MFC中FTP下载功能的实现)