'==================================================================方法一==================================================================
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExsiting As String, ByVal lpszNew As String) As Boolean
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession&, ByVal lpszDirectory$) As Boolean
Dim 连接状态 As Boolean
Dim 连接句柄 As Long
Public Function 连接服务器(IP As String, 端口 As Integer, 帐号 As String, 密码 As String) As Boolean
Dim 初始化 As Long
初始化 = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0&)
连接句柄 = InternetConnect(初始化, IP, 端口, 帐号, 密码, 1, 0, 0)
连接状态 = IIf(连接句柄 > 0, True, False)
连接服务器 = 连接状态
End Function
Public Function 上传文件(本地路径 As String, 远程路径 As String) As Boolean
If 连接状态 = True Then
上传文件 = FtpPutFile(连接句柄, 本地路径, 远程路径, 1, 0)
Else
MsgBox "请先确认连接"
End If
End Function
Public Function 下载文件(远程路径 As String, 本地路径 As String) As Boolean
If 连接状态 = True Then
下载文件 = FtpGetFile(连接句柄, 远程路径, 本地路径, False, &H80000000, &H0, 0)
Else
MsgBox "请先确认连接"
End If
End Function
Public Function 删除文件(ByVal 文件路径) As Boolean
If 连接状态 = True Then
删除文件 = FtpDeleteFile(连接句柄, 文件路径)
Else
MsgBox "请先确认连接"
End If
End Function
Public Function 创建目录(ByVal 路径 As String) As String
If 连接状态 = True Then
创建目录 = FtpCreateDirectory(连接句柄, 路径)
Else
MsgBox "请先确认连接"
End If
End Function
Public Function 文件改名(ByVal 原文件名 As String, ByVal 新文件名 As String)
If 连接状态 = True Then
ReturnVal& = FtpRenameFile(连接句柄, 原文件名, 新文件名)
Else
MsgBox "请先确认连接"
End If
End Function
'==================================================================方法二==================================================================
'须一个inet控件和5个按扭
Private Sub Form_Load()
Inet1.Protocol = icFTP
Inet1.RemoteHost = "174.128.236.185" '服务器地址
Inet1.RemotePort = 21 'FTP端口
Inet1.UserName = 123456 '用户名
Inet1.Password = "******" '密码
End Sub
Private Sub Command1_Click() '上传文件
Inet1.Execute "", "PUT c:\1.jpg /1.jpg"
ri = Inet1.StillExecuting
Do While ri
ri = Inet1.StillExecuting
DoEvents
Loop
End Sub
Private Sub Command2_Click() '(下载)文件
Inet1.Execute "", "GET /1.jpg c:\1.jpg"
ri = Inet1.StillExecuting
Do While ri
ri = Inet1.StillExecuting
DoEvents
Loop
End Sub
Private Sub Command3_Click() '删除文件
Inet1.Execute , "delete 1.jpg"
Do While Inet1.StillExecuting
DoEvents
Loop
End Sub
Private Sub Command4_Click() '删除文件夹(文件夹必须为空)
Inet1.Execute , "rmdir abc"
Do While Inet1.StillExecuting
DoEvents
Loop
End Sub
Private Sub Command5_Click() '新建文件夹
Inet1.Execute , "mkdir abc"
Do While Inet1.StillExecuting
DoEvents
Loop
End Sub