VC创建FTP客户端

        在一个局域网中,我们要通过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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值