Enhanced FTP Code

Here is the enhanced version of the code which sends files to remote computer using FTP which I have posted long back!

Public Function CheckDir(ByVal FolderName As String, ByVal thePath As String) As Boolean
Dim sr As StreamReader = Nothing
Dim dirList() As String = Nothing
Try
Dim myFtpReq As FtpWebRequest = FtpWebRequest.Create(New Uri(thePath))
myFtpReq.Method = WebRequestMethods.Ftp.ListDirectory
myFtpReq.KeepAlive = False
myFtpReq.Proxy = Nothing
myFtpReq.UsePassive = True
myFtpReq.Credentials = New NetworkCredential(userName, paswd)
Dim myFtpResp As FtpWebResponse = CType(myFtpReq.GetResponse(), FtpWebResponse)
Dim ftpRespStream As Stream = myFtpResp.GetResponseStream()
sr = New StreamReader(ftpRespStream, System.Text.Encoding.Default)
Dim folderList As String = sr.ReadToEnd()
‘MsgBox(“Folder List: ” & folderList)
If Not folderList Is Nothing Or folderList <> “” Then
For Each theDir In folderList.Split(vbCrLf)
If theDir.Contains(“/”) Then
If FolderName = theDir.Split(“/”)(1) Then
‘MsgBox(FolderName & ” Folder exist “)
Return True
End If
End If
Next
End If
Return False
Catch ex As Exception
MessageBox.Show(“Error: ” & ex.Message & ” | ” & ex.StackTrace)
Return 0
Finally
If Not sr Is Nothing Then
sr.Close()
sr = Nothing
End If
End Try
End Function

Advertisements