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

本文介绍如何使用MFC实现FTP服务器的文件下载功能。通过创建基于对话框的应用程序,连接到指定的FTP服务器,并展示服务器根目录下的文件列表。此外,还提供了下载指定文件的具体步骤和代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值