用戶登錄  |  用戶注冊
首 頁源碼下載網絡學院最新源碼源碼排行屏蔽廣告
當前位置:新興網絡 > 網絡學院 > 編程開發 > 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類型
秒速时时彩