windows Ftp 操作

来源:互联网 发布:淘宝网套装春秋女装 编辑:程序博客网 时间:2024/06/06 08:50

其实 在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 namepConnect = sess.GetFtpConnection(_T("ftp.microsoft.com"));// use a file find object to enumerate filesCFtpFileFind finder(pConnect);// start loopingBOOL 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 0if (0 == pConnect->GetFile(L"/bussys/Clients/update.txt", L"D:\\cwqUpdate.txt", false)){MessageBox(L"error", L"c");}#elseif (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
原创粉丝点击