在一个局域网中,我们要通过FTP共享要分享文件。我们在遍历FTP共享目录的时候需要注意的是MFC封装的CFtpFileFind一次只能有一个实例,我们在进行递归遍历目录时要注意将其关闭。
直接贴上代码:
// WinApi.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std; #define coutl(XXX) cout<
<
SetCurrentDirectory(strDir); bool bContinue=(bool)finder.FindFile("*"); bool bCreatDir = false; while(bContinue) { bContinue=finder.FindNextFile(); filename=finder.GetFileName(); if(filename=="." || filename=="..") continue; if(finder.IsDirectory()) list.AddTail(filename); else { CString strSrcFileName = filename; filename = strAbDir + filename; if(!bCreatDir) { bCreatDir = true; if(strAbDir != "/") { CString strDirectory = "D:/TestFTP" + strAbDir.Left(strAbDir.GetLength() - 1); coutl("Create Dir:" <
GetFile(strSrcFileName, strDes, FALSE)) { coutl("DownLoad File Ok:" <
SetCurrentDirectory(".."); } } int _tmain(int argc, _TCHAR* argv[]) { string strServer = "192.168.2.116"; string strUser = ""; string strPswd = ""; int nPort = 21; CInternetSession sess(_T("ConnectProgram")); sess.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 3000); CFtpConnection* pConnect = NULL; try { CString strTempServer(strServer.c_str()); CString strTempUser(strUser.c_str()); CString strTempPwd(strPswd.c_str()); pConnect = sess.GetFtpConnection(strTempServer, strTempUser, strTempPwd, nPort); if(NULL == pConnect) { coutl("连接失败"); } else { coutl("连接成功"); CString strDirName; if(pConnect->GetCurrentDirectory(strDirName)) { ExpoloerDir(strDirName, pConnect, strDirName); } } }catch(...) { coutl("建立FTP连接失败"); } //关闭连接 if (pConnect != NULL) { pConnect->Close(); delete pConnect; pConnect = NULL; } sess.Close(); return 0; }