建立基于对话框的MFC应用程序CMfcFtpWinInetDlg:
1、首先Dlg类中包含头文件 #include "afxinet.h"
2、添加成员变量:
1
2 3 |
private:
CFtpConnection* m_pFtpConnection; CInternetSession m_Session; |
3、在OnInitDialog中加入Ftp连接代码:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// TODO: Add extra initialization here
m_pFtpConnection = NULL; try m_pFtpConnection = m_Session.GetFtpConnection(_T("localhost"), _T("Manager"), _T("kingview"), INTERNET_INVALID_PORT_NUMBER); |
4、在OK和Cancel按钮中加入关闭Ftp连接代码:
1
2 3 4 5 6 |
// TODO: Add your control notification handler code here
m_Session.Close(); m_pFtpConnection->Close(); if(m_pFtpConnection != NULL) |
5、添加Upload以及Download按钮代码:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
void CMfcFtpWinInetDlg::OnBnClickedBtnUpload()
{ // TODO: Add your control notification handler code here CFileFind Finder; CString strFileName; // Here c:\\Myfile.bmp is the name of the file that you want if(Finder.FindFile(_T("F:\\hot.gif"))==TRUE) BOOL bUploaded = m_pFtpConnection->PutFile( _T("F:\\hot.gif"), AfxMessageBox(_T("Uploaded Successfully")); void CMfcFtpWinInetDlg::OnBnClickedBtnDownload() |
API参数说明,请参考MSDN。