G
Guest
Hello. I am starting to experiment with the FTP functionality within the .NET
framework, but am having problems producing a list of files.
This is my function which returns files in a string array:
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
Dim strMess As String
Dim intFileCount As Integer
Dim strFiles() As String
m_sMes = ""
If (Not (m_bLoggedIn)) Then
Connect()
End If
cSocket = CreateDataSocket()
'SendCommand("PASV")
SendCommand("NLST " & sMask)
If m_iRetValue = 450 Then
'No files found
Return strFiles
End If
If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
m_sMes = ""
Do While (True)
m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)
bytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)
m_sMes += ASCII.GetString(m_aBuffer, 0, bytes)
If (bytes < m_aBuffer.Length) Then
Exit Do
End If
Loop
mess = m_sMes.Split(seperator)
cSocket.Close()
ReadReply()
If (m_iRetValue <> 226) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
intFileCount = -1
For Each strMess In mess
'example strMess entry
'drwxrws--- 5 tiscali tiscalig 4096 May 2 09:42 CSG
If Left(strMess, 7) <> "drwxrws" And strMess.Length > 0 Then
intFileCount += 1
ReDim Preserve strFiles(intFileCount)
strFiles(intFileCount) = strMess.Trim
End If
Next
Return strFiles
End Function
The first time i try this, it returns many files, however, it only returns
the first file in the directory on subsequent calls. Why is this? The files
are still in the directory, so I'm not sure why it only returns one file
rather than all, the second time I call it?
Is there some sort of clear down i'm meant to be doing that I've missed out?
Thanks.
framework, but am having problems producing a list of files.
This is my function which returns files in a string array:
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
Dim strMess As String
Dim intFileCount As Integer
Dim strFiles() As String
m_sMes = ""
If (Not (m_bLoggedIn)) Then
Connect()
End If
cSocket = CreateDataSocket()
'SendCommand("PASV")
SendCommand("NLST " & sMask)
If m_iRetValue = 450 Then
'No files found
Return strFiles
End If
If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
m_sMes = ""
Do While (True)
m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)
bytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)
m_sMes += ASCII.GetString(m_aBuffer, 0, bytes)
If (bytes < m_aBuffer.Length) Then
Exit Do
End If
Loop
mess = m_sMes.Split(seperator)
cSocket.Close()
ReadReply()
If (m_iRetValue <> 226) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
intFileCount = -1
For Each strMess In mess
'example strMess entry
'drwxrws--- 5 tiscali tiscalig 4096 May 2 09:42 CSG
If Left(strMess, 7) <> "drwxrws" And strMess.Length > 0 Then
intFileCount += 1
ReDim Preserve strFiles(intFileCount)
strFiles(intFileCount) = strMess.Trim
End If
Next
Return strFiles
End Function
The first time i try this, it returns many files, however, it only returns
the first file in the directory on subsequent calls. Why is this? The files
are still in the directory, so I'm not sure why it only returns one file
rather than all, the second time I call it?
Is there some sort of clear down i'm meant to be doing that I've missed out?
Thanks.