windows Ftp 操作

其实 在msdn中就有现成的,拷贝下稍微修改下就成了,需要注意的是

1、目录问题

pConnect->GetCurrentDirectory(dir); 访问路径后 调用此函数获取路径一定是根目录“/”;我们可以直接设置目录,注意此时就不能把前面的<pre name="code" class="cpp">ftp://ftp.microsoft.com 包含上去了;
2、获取文件失败<pre name="code" class="cpp">pConnect->GetFile,首先看下第三个参数,如果设置成true,即不覆盖,那么如果本地路径下已经存在此文件,那么就会返回失败,所以防止这种错误,设置成false;

 
 

void CTestFtpDlg::FtpVist()
{
	CInternetSession sess(_T("My FTP Session"));

	CFtpConnection* pConnect = NULL;

	try
	{
		// Request a connection to ftp.microsoft.com. Default 
		// parameters mean that we'll try with username = ANONYMOUS 
		// and password set to the machine name @ domain name
		pConnect = sess.GetFtpConnection(_T("ftp.microsoft.com"));

		// use a file find object to enumerate files
		CFtpFileFind finder(pConnect);

		// start looping
		BOOL bWorking = finder.FindFile(_T("*"));

		CString str;

		while (bWorking)
		{
			bWorking = finder.FindNextFile();
			//_tprintf_s(_T("%s\n"), (LPCTSTR)finder.GetFileURL());
			str += (LPCTSTR)finder.GetFileURL();
			str += finder.GetFileName();
			str += L"\r\n";
		}
		MessageBox(str, L"123");

		CString dir;
		pConnect->GetCurrentDirectory(dir);  ///< 此时为根目录 “/”,就是所在设备的目录,而不是 ftp://ftp.microsoft.com/bussys/Clients/
		pConnect->SetCurrentDirectory(L"/bussys/Clients/");
		pConnect->GetCurrentDirectory(dir);

#if 0
		if (0 == pConnect->GetFile(L"/bussys/Clients/update.txt", L"D:\\cwqUpdate.txt", false))
		{
			MessageBox(L"error", L"c");
		}
#else
		if (0 == pConnect->GetFile(L"update.txt", L"D:\\cwqUpdate.txt", false))  ///< 即使有也覆盖
		{
			MessageBox(L"error", L"c");
		}
#endif

	}
	catch (CInternetException* pEx)
	{
		TCHAR sz[1024];
		pEx->GetErrorMessage(sz, 1024);
		_tprintf_s(_T("ERROR!  %s\n"), sz);
		pEx->Delete();
	}

	// if the connection is open, close it 
	if (pConnect != NULL)
	{
		pConnect->Close();
		delete pConnect;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值