日期:2009-04-18  浏览次数:20628 次


‘这个类允许你使用Visual Basic.NET对FTP网页执行直接链接。
‘这个类支持以下FTP命令:
‘-Upload 一个文件
‘-Download一个文件
‘-Create 一个目录
‘-Remove 一个目录
‘-改变目录
‘-Remove 一个文件
‘-Rename 一个文件
‘-Set 远程用户的用户名
‘-Set 远程用户的密码

Imports System
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Net.Sockets

‘FTP 类

Public Class clsFTP
#Region "Class Variable Declarations"
Private m_sRemoteHost, m_sRemotePath, m_sRemoteUser As String
Private m_sRemotePassword, m_sMess As String
Private m_iRemotePort, m_iBytes As Int32
Private m_objClientSocket As Socket
Private m_iRetValue As Int32
Private m_bLoggedIn As Boolean
Private m_sMes, m_sReply As String

‘设置用户来对FTP服务器读取和写入数据的数据包的大小
‘对下列具体大小

Public Const BLOCK_SIZE = 512
Private m_aBuffer(BLOCK_SIZE) As Byte
Private ASCII As Encoding = Encoding.ASCII
Public flag_bool As Boolean
‘普通变量定义
Private m_sMessageString As String
#End Region

#Region "Class Constructors"

‘Main类的构造器
Public Sub New()
 m_sRemoteHost = "microsoft"
 m_sRemotePath = "."
 m_sRemoteUser = "anonymous"
 m_sRemotePassword = ""
 m_sMessageString = ""
 m_iRemotePort = 21
 m_bLoggedIn = False
End Sub

‘参数化的构造器

Public Sub New(ByVal sRemoteHost As String, _
ByVal sRemotePath As String, _
ByVal sRemoteUser As String, _
ByVal sRemotePassword As String, _
ByVal iRemotePort As Int32)

 m_sRemoteHost = sRemoteHost
 m_sRemotePath = sRemotePath
 m_sRemoteUser = sRemoteUser
 m_sRemotePassword = sRemotePassword
 m_sMessageString = ""
 m_iRemotePort = 21
 m_bLoggedIn = False

End Sub

#End Region

#Region "Public Properties"

‘设置或得到你想链接的FTP服务器的名称

Public Property RemoteHostFTPServer() As String
 ‘得到FTP服务器的名称
 Get
  Return m_sRemoteHost
 End Get

‘设置FTP服务器的名称

 Set(ByVal Value As String)
  m_sRemoteHost = Value
 End Set
End Property



‘设置或得到你想链接的FTP服务器的FTP端口

Public Property RemotePort() As Int32

‘得到FTP端口号

 Get
  Return m_iRemotePort
 End Get

‘设置FTP端口数号

 Set(ByVal Value As Int32)
  m_iRemotePort = Value
 End Set
End Property

‘设置或得到你想链接的FTP服务器的远程路径

Public Property RemotePath() As String

 ‘得到远程路径
 Get
  Return m_sRemotePath
 End Get
 ‘设置远程路径
 Set(ByVal Value As String)
  m_sRemotePath = Value
 End Set
End Property

‘设置你想链接的远程FTP服务器的密码

Public Property RemotePassword() As String
 Get
  Return m_sRemotePassword
 End Get
 Set(ByVal Value As String)
  m_sRemotePassword = Value
 End Set
End Property

‘设置或得到你想链接远程的FTP服务器的用户

Public Property RemoteUser() As String
 Get
  Return m_sRemoteUser
 End Get

 Set(ByVal Value As String)
  m_sRemoteUser = Value
 End Set
End Property

‘设置messagestring类

Public Property MessageString() As String
 Get
  Return m_sMessageString
 End Get

 Set(ByVal Value As String)
  m_sMessageString = Value
 End Set
End Property

#End Region
#Region "Public Subs and Functions"

‘从文件系统中返回一个文件列表。在string()函数中返回文件。

Public Function GetFileList(ByVal sMask As String) As String()
 Dim cSocket As Socket
 Dim bytes As Int32
 Dim seperator As Char = ControlChars.Lf
 Dim mess() As String

 m_sMes = ""
‘检查你是否登录到FTP服务器上
 If (Not (m_bLoggedIn)) Then
  Login()
 End If

 cSocket = CreateDataSocket()
‘发送FTP命令

 SendCommand("NLST " & sMask)
 If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
  MessageString = m_sReply
  Throw New IOException(m_sReply.Substring(4))
 End If
 m_sMes = ""