Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
5.2k views
in .NET FTP by (51.1k points)
An edtFTPnet user asks:

Using the following code I am trying to display the dirs only in a listview but when I try to run it an error message pops up saying that its not connected yet. I am using the isconnected as boolean to keep Dirs from being displayed until I am connected? Its important to note if I dispaly all folders and files it works fine. I dont have the problem until I try to display only Folders.

Friend WithEvents eFTPConnection As New EnterpriseDT.Net.Ftp.FTPConnection
 
Try
            Logger.CurrentLevel = Level.ALL
            Dim myappender As Appender = New StatusBoxAppender(txtResponse)
            Logger.AddAppender(myappender)
 
            Cursor = Cursors.WaitCursor
            txtResponse.Text = "Attempting to contact the ftp server. Please Wait"
            'Dim Con As New EnterpriseDT.Net.Ftp.FTPConnection
            With eFTPConnection
                UserName = txtUsername.Text
                Password = txtPassword.Text
                ServerAddress = txtServer.Text
                ServerPort = CInt(nudPort.Value)
                Connect()
                If eFTPConnection.IsConnected Then
                    Dim Dir As String
                    txtResponse.Text = " Sucessfully connected to .." & txtServer.Text
                    txtResponse.Text = "Retrieving Files from ftp server.." & vbCrLf
                    For Each Dir In eFTPClient.Dir
                        Dim lvDetails As New ListViewItem
                        lvDetails.ImageIndex = 0
                        lvDetails.SubItems(0).Text = Dir
                        lvDirs.Items.Add(lvDetails)
                    Next
                End If
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Cursor = Cursors.Default


You should either be connected after you call Connect() or an exception should've been thrown. Can you please post the relevant portion of the log?

- Hans (EDT)

8 Answers

0 votes
by (600 points)
I dont have logging on yet. However it is throwing an exception:

The FTP Client has not yet connected to the server. The requested action cannot be performed until after a connection has been established.
0 votes
by (600 points)
Looking closer at the code I provided I see where the problem lies but I am not sure how to correct it. I am using eftConnection which is the connection object and further down I use the ftpClient to get the array of Dirs ftpClient is not connected but the ftpconnection is. Looking at the object browser I see that ftpConnection class doesnt support the dir array and I cant seem to get the ftpClient connected properly to use the array I need to spell out the dirs.
0 votes
by (51.1k points)
Oh, I didn't notice that. Easily fixed. Use FTPConnection.GetFiles() instead of FTPClient.Dir(). We've renamed many of the methods in FTPConnection to be more .NET-like and less FTP-like.

- Hans (EDT)
0 votes
by (600 points)
Hans I realize that the getfiles does work call me dumb if you will but I cant seem to figure out with the getfiles() if it is a directory or a file. At Least with ftpClient it was not an issue because it used the isdir as boolean. am I missing something here is there a command that lets me know that it is a directory ex if file = <DIR> then it is a directory. Maybe you should impliment the isDirectory it would save a lot of confusion for people like me thanks.
0 votes
by (51.1k points)
Use FTPConnection.GetFileInfos() which returns an array of FTPFile object. FTPFile has a member, Dir, which will tell you whether or not the entry is a directory. I just realized that some of the documentation for FTPFile is missing. We'll fix this soon.

- Hans (EDT)
0 votes
by (600 points)
I spent hours on this and after searching the forum I found figured out how to connect using the FtpClient so I could use the Dir I thought I had it figured out but still cant get it to work I commented the code so that you could see what I am trying to do.
        Dim File As String
        Dim myFTP As New EnterpriseDT.Net.Ftp.FTPClient

        With myFTP
            'Connect client To ftp server
            .RemoteHost = "productfast.com"
            .ConnectMode = EnterpriseDT.Net.Ftp.FTPConnectMode.PASV
            .Connect()
            .Login(txtUsername.Text, txtPassword.Text)
            'Loop through the entire file and directories listing
            For Each File In myFTP.Dir
                'Check to see if file is a directory if so add it to the listbox
                If File Is Dir(File) Then
                    ListBox1.Items.Add(File)
                End If
            Next

The listbox displays nothing and doent error out. I tried to use the dirDetails and it did list the folders but I couldnt get it to break down to the foldername alone. I am about to give it up. any suggestions. I think some of the confusion is in the dir() command because I originally thought that the array was only Directories but when I went through the array it had the whole Directory including files.
0 votes
by (51.1k points)
Hi Dwain

You can do it using FTPClient and its DirDetails method, but we are recommending use of FTPConnection. Here's the code to achieve what you want using FTPConnection:
        Dim myFile As EnterpriseDT.Net.Ftp.FTPFile
        Dim myFTP As New EnterpriseDT.Net.Ftp.FTPConnection

        With myFTP

            ' Set up connection properties
            .ServerAddress = txtServerAddress.Text
            .UserName = txtUsername.Text
            .Password = txtPassword.Text

            ' Connect to the server
            .Connect()

            ' Loop through the entire file and directories listing
            For Each myFile In myFTP.GetFileInfos

                ' Check to see if file is a directory if so add its name to the listbox
                If myFile.Dir Then
                    lstFiles.Items.Add(myFile.Name)
                End If

            Next

            ' Don't forget to close the connection
            .Close()

        End With


- Hans (EDT)
0 votes
by (600 points)
Hans Thank you very much it works great. When I am ready I will upgrade to the pro version. Thanks Dwain

Categories

...