FTP编程的小问题

lltaotao007 2005-08-10 01:10:39
关于FTP编程操作的三个问题:
1如何判断目录是否存在
2如何得到目录下所有文件名
3如何返回上级目录(如返回根目录)
...全文
306 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wbczyh 2005-08-10
  • 打赏
  • 举报
回复
CSDN可以让用户自己删除贴子吗?
wbczyh 2005-08-10
  • 打赏
  • 举报
回复
对不起,我看错了,这里关于VC的。
wbczyh 2005-08-10
  • 打赏
  • 举报
回复
使用以的类模块:
将以内容保存为一个文件:clsFTP.cls
由于太长不能在一个贴子放下,所以分了几部分:
Part1:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsFTP"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'Copyright Graham Daly December 2001

' ---------------------------------------------------------
' ------------------- API Functions --------------------
' ---------------------------------------------------------

'File time information
Private Type FileTime
dwLowDateTime As Long
dwHighDateTime As Long
End Type

'File information
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FileTime
ftLastAccessTime As FileTime
ftLastWriteTime As FileTime
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * 260
cAlternate As String * 14
End Type

'Wininet.dll API's
Private Declare Function FtpFindFirstFile& Lib "wininet.dll" _
Alias "FtpFindFirstFileA" _
(ByVal hOutboundSession&, _
ByVal lpszSearchFile$, _
lpFindFileData As WIN32_FIND_DATA, _
ByVal dwFlags&, _
ByVal dwContent&)

Private Declare Function InternetFindNextFile& Lib "wininet.dll" _
Alias "InternetFindNextFileA" _
(ByVal hFind&, _
lpvFindData As WIN32_FIND_DATA)

Private Declare Function FtpGetFile Lib "wininet.dll" _
Alias "FtpGetFileA" _
(ByVal hOutboundSession&, _
ByVal lpszRemoteFile$, _
ByVal lpszNewFile$, _
ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes&, _
ByVal dwFlags&, _
ByVal dwContext&) As Boolean

Private Declare Function FtpPutFile Lib "wininet.dll" _
Alias "FtpPutFileA" _
(ByVal hOutboundSession&, _
ByVal lpszLocalFile$, _
ByVal lpszRemoteFile$, _
ByVal dwFlags&, _
ByVal dwContext&) As Boolean

Private Declare Function FtpRenameFile Lib "wininet.dll" _
Alias "FtpRenameFileA" _
(ByVal hOutboundSession&, _
ByVal sExistingName$, _
ByVal sNewName$) As Boolean

Private Declare Function FtpDeleteFile Lib "wininet.dll" _
Alias "FtpDeleteFileA" _
(ByVal hOutboundSession&, _
ByVal lpszFileName$) As Boolean

Private Declare Function FtpGetCurrentDirectory& Lib "wininet.dll" _
Alias "FtpGetCurrentDirectoryA" _
(ByVal hConnect&, _
ByVal lpszCurrentDirectory$, _
lpdwCurrentDirectory&)

Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" _
Alias "FtpSetCurrentDirectoryA" _
(ByVal hOutboundSession&, _
ByVal lpszDirectory$) As Boolean

Private Declare Function InternetConnect& Lib "wininet.dll" _
Alias "InternetConnectA" _
(ByVal hInternetSession&, _
ByVal sServerName$, _
ByVal nServerPort%, _
ByVal sUserName$, _
ByVal sPassword$, _
ByVal lService&, _
ByVal lFlags&, _
ByVal lContext&)

Private Declare Function InternetOpen& Lib "wininet.dll" _
Alias "InternetOpenA" _
(ByVal sAgent$, _
ByVal lAccessType&, _
ByVal sProxyName$, _
ByVal sProxyBypass$, _
ByVal lFlags&)

Private Declare Function InternetCloseHandle& Lib "wininet.dll" _
(ByVal hInet&)

Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" _
Alias "InternetGetLastResponseInfoA" _
(lpdwError&, _
ByVal lpszBuffer$, _
lpdwBufferLength&) As Boolean

Private Declare Function FileTimeToSystemTime Lib "kernel32" _
(lpFileTime As FileTime, _
lpSystemTime As SYSTEMTIME) As Long

' ---------------------------------------------------------
' ---------------- Module-level Constants -----------------
' ---------------------------------------------------------

Private Const FAILURE& = 0
Private Const SUCCESS& = 1

'Error messages
Private Const ERR_CHANGE_DIR$ = "Cannot change directory to <%s>. It either doesn't exist, or is protected"
Private Const ERR_CONNECT_ERROR$ = "Cannot connect to FTP server <%s> using User and Password Parameters"
Private Const ERR_ALREADY_CONNECTED$ = "Cannot change property while connected to FTP server"
Private Const ERR_NO_CONNECTION$ = "Cannot connect to FTP server"
Private Const ERR_DOWNLOAD$ = "Cannot get file <%s> from FTP server"
Private Const ERR_RENAME$ = "Cannot rename file <%s>"
Private Const ERR_DELETE$ = "Cannot delete file <%s> from FTP server"
Private Const ERR_CRITICAL$ = "Cannot get connection to WinInet.dll!"

'Type of service to access
Private Const INTERNET_SERVICE_FTP = 1

'Flags
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
Private Const INTERNET_FLAG_MULTIPART = &H200000
Private Const INTERNET_FLAG_PASSIVE = &H8000000
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
Private Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000

'File Transfer modes
Private Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Private Const FTP_TRANSFER_TYPE_ASCII = &H1
Private Const FTP_TRANSFER_TYPE_BINARY = &H2

'Other
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const INET_SESSION_NAME$ = "ICB FTP Sesh"
Private Const ERROR_INTERNET_EXTENDED_ERROR = 12003
Private Const NO_CONNECTION& = FAILURE&
Private Const FWDSLASH$ = "/"
Private Const BACKSLASH$ = "\"

18,357

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧