⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpfiledlg.cpp

📁 使用WinInet类开发的ftp客户端 实现了ftp的基本功能.
💻 CPP
字号:
// FtpFileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FtpFile.h"
#include "FtpFileDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFtpFileDlg dialog

CFtpFileDlg::CFtpFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFtpFileDlg::IDD, pParent)
	, m_strFtpInfo(_T(""))
	, m_strHost(_T("162.105.25.111"))
	, m_strUserName(_T(""))
	, m_strPassword(_T(""))
	, m_nPort(21)
{
	//{{AFX_DATA_INIT(CFtpFileDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFtpFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFtpFileDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	DDX_Text(pDX, IDC_EDIT1, m_strFtpInfo);
	DDX_Text(pDX, IDC_EDIT2, m_strHost);
	DDX_Text(pDX, IDC_EDIT3, m_strUserName);
	DDX_Text(pDX, IDC_EDIT4, m_strPassword);
	DDX_Text(pDX, IDC_EDIT5, m_nPort);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFtpFileDlg, CDialog)
	//{{AFX_MSG_MAP(CFtpFileDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_CHANGE(IDC_EDIT1, OnEnChangeEdit1)
	ON_BN_CLICKED(IDC_BUTTON_LIST, OnBnClickedButtonList)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFtpFileDlg message handlers

BOOL CFtpFileDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_nIndex=0;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFtpFileDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFtpFileDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFtpFileDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CFtpFileDlg::OnEnChangeEdit1()
{
	return;
}

void CFtpFileDlg::OnBnClickedButtonList()
{
	m_pInetSession=new CInternetSession(AfxGetAppName(),
		1,PRE_CONFIG_INTERNET_ACCESS);
	UpdateData(TRUE);

	try 
	{
		m_pFtpConnection=m_pInetSession->GetFtpConnection(m_strHost,
			m_strUserName,m_strPassword,m_nPort);
    
		if(m_pFtpConnection != NULL)
		{
			CString m_strMsg;
			m_strMsg="连接成功\r\n";
		    m_strFtpInfo +=m_strMsg;
		    UpdateData(FALSE);
		
			FileList();
     	}
	}
	catch(CInternetException * pEx)
	{
		CString m_strMsg;
		m_strMsg="连接no成功\r\n";
		m_strFtpInfo +=m_strMsg;
		UpdateData(FALSE);
		TCHAR szError[1024];
		if(pEx->GetErrorMessage(szError,1024)) 
		{
			m_strMsg=(CString)szError;
			m_strFtpInfo +=m_strMsg;
			UpdateData(FALSE);
		}
		else 
			AfxMessageBox("There was an exception");
		pEx->Delete();
		m_pFtpConnection=NULL;
	}
}

void CFtpFileDlg::OnDestroy()
{
	CDialog::OnDestroy();
}

void CFtpFileDlg::FileList()
{
	CString m_strMsg;
	CString m_strMsgAdd;	//用来保存一些临时信息
	CStringArray m_saDir;	//用数组来保存目录

	CFtpFileFind   finder(m_pFtpConnection);
    BOOL bWorking=finder.FindFile(_T("*"));
	while (bWorking)
    {
		bWorking = finder.FindNextFile();

		if (finder.IsDots()) 
			continue;
		
		if (finder.IsDirectory())
		{  
			m_saDir.Add(finder.GetFileName());	//如果是目录的话,就保存在数组里
		}
		else
		{
			m_strMsg=finder.GetFileName()+"\r\n";	//如果不是目录的话,就先显示出来
			for (int j=0;j<m_nIndex;j++)	//这里就是来控制层次的
			{
				m_strMsgAdd="\t";
				m_strFtpInfo=m_strFtpInfo+m_strMsgAdd;
			}
			
			m_strFtpInfo +=m_strMsg;
			UpdateData(FALSE);
		}
	}
	finder.Close();		//连接关闭
	
	for(int i=0;i<m_saDir.GetSize();i++)	//开始遍历目录里的文件
	{	
		m_nIndex++;
		m_strMsg="["+m_saDir.GetAt(i)+"]"+"\r\n";	//在目录的名字外面加上[]
		
		for(int j=1;j<m_nIndex;j++)
		{
			m_strMsgAdd="\t";
			m_strFtpInfo=m_strFtpInfo+m_strMsgAdd;
		}		
		m_strFtpInfo +=m_strMsg;	//先显示目录的名字
		UpdateData(FALSE);	
		
		BOOL m_bSuc=FALSE;
		while (!m_bSuc)		//进入目录里面
		{
			m_bSuc=m_pFtpConnection->SetCurrentDirectory(m_saDir.GetAt(i));
		}
			 		  
		FileList();			//递归调用!
		
		m_bSuc=FALSE;
		while(!m_bSuc)
		{
			m_bSuc= m_pFtpConnection->SetCurrentDirectory("..");
		}		
		m_nIndex--;			//控制层次
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -