How to show FOLDERS that stored in FTP SERVER.

SuperShinta

Member
Joined
Oct 8, 2011
Messages
12
Programming Experience
Beginner
I want to know how to show FOLDERS in my FTP Upload and Download System, It only appears files but folders doesn't show up. Please help here's the code:
VB.NET:
Imports System.Net
Imports System.IO
Public Class Form4


    Private Sub listFTP(ByVal URL As String, ByVal UserName As String, ByVal Password As String)


        Dim requ As FtpWebRequest = Nothing
        Dim resp As FtpWebResponse = Nothing
        Dim reader As StreamReader = Nothing
        Try
            requ = CType(WebRequest.Create(URL), WebRequest)
            requ.Credentials = New NetworkCredential(UserName, Password)
            requ.Method = WebRequestMethods.Ftp.ListDirectory
            resp = CType(requ.GetResponse(), FtpWebResponse)
            reader = New StreamReader(resp.GetResponseStream())
            While (reader.Peek() > -1)
                lstFileList.Items.Add(reader.ReadLine())
            End While


        Catch ex As UriFormatException
            MsgBox("Please check the information you've entered.", MsgBoxStyle.Critical, "Error")
        Catch ex As WebException
            MsgBox("Please check the information you've entered.", MsgBoxStyle.Critical, "Error")
        Finally
            If reader IsNot Nothing Then reader.Close()
        End Try
    End Sub


    Private Sub downloadFTP(ByVal URL As String, ByVal UserName As String, ByVal Password As String)


     Dim requ As FtpWebRequest = Nothing
        Dim resp As FtpWebResponse = Nothing
        Dim respStrm As Stream = Nothing
        Dim fileStrm As FileStream = Nothing


        


        Try
            requ = CType(WebRequest.Create(URL), FtpWebRequest)
            requ.Credentials = New NetworkCredential(UserName, Password)
            requ.Method = WebRequestMethods.Ftp.DownloadFile
            resp = CType(requ.GetResponse(), FtpWebResponse)
            respStrm = resp.GetResponseStream()
            SaveFileDialog1.FileName = Path.GetFileName(requ.RequestUri.LocalPath)
            If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                fileStrm = File.Create(SaveFileDialog1.FileName)
                Dim buff(1024) As Byte
                Dim bytesRead As Integer = 0
                While (True)
                    bytesRead = respStrm.Read(buff, 0, buff.Length)
                    If (bytesRead = 0) Then Exit While
                    fileStrm.Write(buff, 0, bytesRead)
                End While
                MsgBox("Download Complete!                                        ")
            End If
        Catch ex As UriFormatException
            MsgBox("Please check the information you've entered.", MsgBoxStyle.Critical, "Error")
        Catch ex As WebException
            MsgBox("Please check the information you've entered.", MsgBoxStyle.Critical, "Error")
        Finally
            If respStrm IsNot Nothing Then respStrm.Close()
            If fileStrm IsNot Nothing Then fileStrm.Close()
        End Try
    End Sub


    Private Sub cmdList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdList.Click
        Try
            lstFileList.Items.Clear()
            listFTP(txturl.Text, txtusername.Text, txtpassword.Text)
        Catch
            MsgBox("Please check the information you've entered.", MsgBoxStyle.Critical, "Error")


        End Try
    End Sub


    Private Sub cmdDowload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDownload.Click
        downloadFTP(TextBox1.Text, txtusername.Text, txtpassword.Text)


    End Sub


    Private Sub lstFileList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstFileList.SelectedIndexChanged
        TextBox1.Text = txturl.Text & "/" & lstFileList.SelectedItems(0).ToString()
    End Sub


    Private Sub deleteFTP(ByVal URL As String, ByVal UserName As String, ByVal Password As String)
        Dim requ As FtpWebRequest = Nothing
        Dim resp As FtpWebResponse = Nothing
        Try
            requ = CType(WebRequest.Create(URL), FtpWebRequest)
            requ.Credentials = New NetworkCredential(UserName, Password)
            requ.Method = WebRequestMethods.Ftp.DeleteFile
            resp = CType(requ.GetResponse(), FtpWebResponse)


        Catch ex As Exception
            MsgBox("Nothing to delete.", MsgBoxStyle.Critical, "Error")
        Finally
            If resp IsNot Nothing Then resp.Close()
        End Try




    End Sub
 
    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Try
            If (MessageBox.Show("Are you sure you want to delete this file?", "Confirm File Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
                deleteFTP(TextBox1.Text, txtusername.Text, txtpassword.Text)
                lstFileList.Items.Clear()
                listFTP(txturl.Text, txtusername.Text, txtpassword.Text)
            End If


        Catch




        End Try
    End Sub


    Private Sub txtpassword_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpassword.TextChanged
        AcceptButton = cmdList
    End Sub


    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        Form3.Show()
        Me.Close()
    End Sub


    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim answer As String
        answer = MessageBox.Show("Are you sure you want to cancel the operation?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If (answer = vbYes) Then
            End
        End If
    End Sub


    Private Sub LinkLabel3_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel3.LinkClicked
        Form1.Show()
    End Sub
End Class

Thanks in advance :)
 
ListDirectory provides a name list of files and folders, ListDirectoryDetails provides a formatted list with more details about same items.
 
ListDirectory provides a name list of files and folders, ListDirectoryDetails provides a formatted list with more details about same items.

Thanks Mr. John H. I added this ListDirectoryDetails and appears the foldernames, time from ftp server but some words appears that i dont know where came from. And The alignment of the texts, i hope it can show icons and align it to the left side.
 
Back
Top