使用wininet枚举FTP目录

今天简单实现了一下枚举FTP目录的功能,不过有个前提是FTP服务器对我们即将使用的帐号开放LIST权限,不然我们没有权限访问。代码如下:

/*
@param1:strHostName:FTP服务器IP或域名
@param2:nPort:FTP端口号
@param3:strUser:FTP用户名
@param4:strPwd:FTP密码
@param5:strDir:要访问的FTP目录,默认为根目录,可以用FtpSetCurrentDirectory改变
*/
BOOL EnumFtp(CString strHostName,int nPort,CString strUser,CString strPwd,CString strDir="\\")
{
 HINTERNET hSession = NULL;
 HINTERNET hConnection = NULL;
 try
 {
  hSession = InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
  if (!hSession)
   throw "error";
  hConnection = InternetConnect(hSession,strHostName,nPort,strUser,strPwd,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
  if (!hConnection)
   throw "error";

  FtpSetCurrentDirectory(hConnection,strDir);

  WIN32_FIND_DATA wfd;
  HINTERNET hFind = FtpFindFirstFile(hConnection,_T("*.*"),&wfd,INTERNET_FLAG_DONT_CACHE,NULL);
  if (hFind != NULL)
  {
   do
   {
    if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    {
     std::cout<<"文件名:"<<wfd.cFileName<<std::endl;
    }
    else
    {
     std::cout<<"目录名:"<<wfd.cFileName<<std::endl;
    }
   } while (InternetFindNextFile(hFind,&wfd));
   
   InternetCloseHandle(hFind);
  }

  if (hConnection != NULL)
   InternetCloseHandle(hConnection);
  if (hSession != NULL)
   InternetCloseHandle(hSession);

  return TRUE;

 }
 catch(...)
 {
  if (hConnection != NULL)
   InternetCloseHandle(hConnection);
  if (hSession != NULL)
   InternetCloseHandle(hSession);
  return FALSE;
 }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值