📄 myftpdlg.cpp
字号:
CString errStr;
DWORD dw = GetLastError();
errStr.Format("创建目录失败.Error Code: %u",dw);
MessageBox(errStr);
}
else
{
MessageBox("创建目录成功!");
}
return;
}
void CMyFtpDlg::OnCurdir()
{
if (m_pFtpConnection)
{
if (!m_pFtpConnection->GetCurrentDirectory(m_DirName))
{
MessageBox("获取目录失败.");
}
else
{
UpdateData(FALSE);//在编辑框中显示当前目录
}
}
else
{
MessageBox("还没有连接到FTP服务器。");
}
}
void CMyFtpDlg::OnChangdir()
{
UpdateData();
m_DirName.TrimLeft();
m_DirName.TrimRight();
if (m_pFtpConnection)
{
if (!m_pFtpConnection->SetCurrentDirectory(m_DirName))
{
MessageBox("改变目录失败.");
}
else
{
if (!(m_FileList.DeleteAllItems()))
{ MessageBox("改变目录失败.");return;}
else List();
}
}
else
{
MessageBox("还没有连接到FTP服务器。");
}
}
void CMyFtpDlg::List()
{
CString strName, strLen, strType,strTime;
CString strAttr,strAttr1,strAttr2,strAttr3,strAttr4;
FILETIME ftWrite;
DWORD dwLen;
BOOL bWorking=TRUE;
int nItem;
char strDir[120];
DWORD dSize=120;
CFtpFileFind finder(m_pFtpConnection);
m_pFtpConnection->GetCurrentDirectory(strDir,&dSize);
m_DirName.Format("%s",strDir);
try
{
bWorking=finder.FindFile(_T("*"));
while (bWorking)
{
bWorking = finder.FindNextFile();
strName=finder.GetFileName();//名称
if (!(finder.GetLastWriteTime(&ftWrite))) strTime="";//更改时间
else strTime=GetTimeStr(ftWrite);
if (finder.IsDirectory()) strType="文件夹";//类型
else strType="文件";
//大小...begin
dwLen=finder.GetLength();
if (dwLen<1024L) strLen.Format("%ldBytes",dwLen);
else
{
dwLen/=1024L;
if (dwLen<1024L) strLen.Format("%ldKB",dwLen);
else
{dwLen/=1024L;
strLen.Format("%ldMB",dwLen);
}
//大小...end
//属性...begin
if (finder.IsDirectory()) strAttr1="d";
else strAttr1="-";//是否目录
strAttr2="r";//是否可读
if (finder.IsReadOnly()) strAttr3="-";//是否可写
else strAttr3="w";
strAttr4="x";//是否可执行
strAttr=strAttr1+strAttr2+strAttr3+strAttr4;
//属性...end
// 往列表控件中添加项目 ...begin
nItem = m_FileList.InsertItem(iItemCount, strName); // 名称
m_FileList.SetItemText(nItem, 1,strLen); // 大小
m_FileList.SetItemText(nItem, 2,strType); // 类型
m_FileList.SetItemText(nItem, 3,strTime); // 更改时间
m_FileList.SetItemText(nItem, 4,strAttr);//属性
// 往列表控件中添加项目 ...end
}
iItemCount++;
}
}
catch (CInternetException* pEx)
{
TCHAR sz[1024];
pEx->GetErrorMessage(sz, 1024);
printf("ERROR! %s\n", sz);
pEx->Delete();
}
UpdateData(FALSE);
finder.Close();
return;
}
CString CMyFtpDlg::GetTimeStr(FILETIME ftWrite)
{
FILETIME ftLocal;
CString strRet;
CString strTmp;
int iLen;
SYSTEMTIME sysTime;
// Convert the last-write time to local time.
if (!FileTimeToLocalFileTime(&ftWrite, &ftLocal))
{strRet="";return strRet;}
// Convert the local file time from UTC to system time.
FileTimeToSystemTime(&ftLocal, &sysTime);
strTmp.Format("%d-%d-%d ",sysTime.wYear,sysTime.wMonth,
sysTime.wDay);
iLen=strTmp.GetLength();
if (iLen==9) strTmp.Insert(9," ");
if (iLen==10) strTmp.Insert(10," ");
strRet=strTmp;
strTmp.Format("%d:%02d:%02d",sysTime.wHour,
sysTime.wMinute,sysTime.wSecond);
strRet+=strTmp;
return strRet;
}
void CMyFtpDlg::OnDblclkList(NMHDR* pNMHDR, LRESULT* pResult)
{
if (!bConnect) //如果连接已经断开,重新连接
{
bFirstConnect=FALSE;
OnConnect();
}
POSITION pos;
CString strName,strType;
pos = m_FileList.GetFirstSelectedItemPosition();
IndexInFieldList = m_FileList.GetNextSelectedItem(pos); // 得到项目索引
strName = m_FileList.GetItemText(IndexInFieldList, 0);//名称
strType=m_FileList.GetItemText(IndexInFieldList, 2);//类型
if (strType=="文件") //如果双击的是文件,就下载该文件
{
DownLoadFile(strName);
return;
}
//如果是文件夹,就将该文件夹设为当前目录
strName = m_FileList.GetItemText(IndexInFieldList, 0);
OnCurdir();//得到当前目录m_DirName
int strLen=m_DirName.GetLength();
if (m_DirName.GetAt(strLen-1)=='/')
m_DirName=m_DirName+strName;
else m_DirName=m_DirName+"/"+strName;//将双击的文件夹设为当前目录
UpdateData(FALSE);//更新显示
OnChangdir();//设置服务器的当前目录
*pResult = 0;
}
void CMyFtpDlg::OnFatherdir()
{
int iLastFlash;
if (!bConnect)
{
bFirstConnect=FALSE;
OnConnect();
}
if (m_DirName=="/") return;
iLastFlash=m_DirName.ReverseFind('/');
if (iLastFlash==0) m_DirName="/";
else m_DirName=m_DirName.Left(iLastFlash);
UpdateData(FALSE);
OnChangdir();
}
HWND CMyFtpDlg::CreateTipDlg()
{
CTipDlg* pDialog;
HWND hWndRet;
//pDialog initialized to NULL in the constructor of CMyWnd class
pDialog = new CTipDlg();
//Check if new succeeded and we got a valid pointer to a dialog object
if(pDialog != NULL)
{
BOOL ret = pDialog->Create(IDD_TIP_DIALOG,this);
if(!ret) //Create failed.
AfxMessageBox("Error creating Tip Dialog");
else
{
pDialog->ShowWindow(SW_SHOW);
// Sleep(50000);
hWndRet=pDialog->m_hWnd;
}
}
else
AfxMessageBox("Error Creating Tip Dialog Object");
return hWndRet;
}
void CMyFtpDlg::DownLoadFile(CString strFileName)
{
HWND hTipWnd;
hTipWnd=CreateTipDlg();//创建一个文件下载提示对话框
if (m_pFtpConnection) //建立了FTP连接
{
if (!m_pFtpConnection->GetFile(strFileName,"c:\\" + strFileName))//下载失败
{
::SendMessage(hTipWnd,WM_MY_EXIT,1,0);//文件下载提示对话框中提示下载失败
}
else//下载完成
{
::SendMessage(hTipWnd,WM_MY_EXIT,0,0);//文件下载提示对话框中提示下载完成
}
}
else//没有建立FTP连接
{
MessageBox("还没有连接到FTP服务器。");
}
}
void CMyFtpDlg::OnDel()
{
POSITION pos;
CString strName,strType;
pos = m_FileList.GetFirstSelectedItemPosition();
IndexInFieldList = m_FileList.GetNextSelectedItem(pos); // 得到项目索引
strName = m_FileList.GetItemText(IndexInFieldList, 0);//名称
strType=m_FileList.GetItemText(IndexInFieldList, 2);//类型
if (strType=="文件") Deletefile(strName);
else Deletedir(strName);
}
void CMyFtpDlg::Deletefile(CString strFileToDel)
{
UpdateData();
if (m_pFtpConnection)
{
if (!m_pFtpConnection->Remove(strFileToDel))
{
CString errStr;
DWORD dw = GetLastError();
errStr.Format("删除文件失败.Error Code: %u",dw);
MessageBox(errStr);
}
else
{
MessageBox("删除文件成功!");
}
}
else
{
MessageBox("还没有连接到FTP服务器。");
}
}
void CMyFtpDlg::Deletedir(CString strDirToDel)
{
UpdateData();
if (m_pFtpConnection)
{
if (!m_pFtpConnection->RemoveDirectory(strDirToDel))
{
CString errStr;
DWORD dw = GetLastError();
errStr.Format("删除目录失败.Error Code: %u",dw);
MessageBox(errStr);
}
else
{
MessageBox("删除目录成功!");
}
}
else
{
MessageBox("还没有连接到FTP服务器。");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -