用户登录  |  用户注册
首 页源码下载网络学院最新源码源码排行屏蔽广告
当前位置:新兴网络 > 网络学院 > 编程开发 > Visual Basic

VB API实现FTP上传、下载、创建目录、删除文件、改名类模块

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2010-09-19 09:00:15
VB FTP操作类示例.rar

调用示例:

Private Sub Form_Load()
    Dim F As FTP
    Set F = New FTP

    Call F.连接服务器("127.0.0.1", 21, "admin", "admin888")

    Call F.下载文件("/wwwroot/index.php", "c:\test.php")
End Sub

FTP类模块
cls:

VBScript code复制代码
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)
    If 连接句柄 > 0 Then
        连接状态 = True
    Else
        连接状态 = False
    End If
    连接服务器 = 连接状态
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
  • 好的评价 如果您觉得此文章好,就请您
      92%(12)
  • 差的评价 如果您觉得此文章差,就请您
      8%(1)

网络学院评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 1 条,得分 85 分,平均 85 分) 查看完整评论
[回复] 1游客   打分:85 分  发表时间:2013-08-21
· 没有FTP类型