tags:

views:

204

answers:

1

Hi, I'm trying to figure out how to list all files of an FTP folder in a listview but I can't figure out how to do this. I tried using the same approach as if I were to list contents of a folder in my harddrive. But this fails.

Any ideas?

I thought of something like this (in vb.net, but I'm sure c# is pretty similar):

    Public Sub FTPFill()

    Dim ftpreq As FtpWebRequest = FtpWebRequest.Create("ftp://FTPRoot/")
    With ftpreq
        .Credentials = New NetworkCredential(My.Settings.FTPUsername, My.Settings.FTPPassword)
        .Method = WebRequestMethods.Ftp.ListDirectory
    End With

    Dim sr As New StreamReader(ftpreq.GetResponse().GetResponseStream())
    Dim str As String = sr.ReadLine()


    For Each str In sr.ReadLine()

        Dim lvi As New ListViewItem(sr.ReadLine())
        lvi.ImageIndex = 0
        lvi.SubItems.Add("Folder")
        lvi.SubItems.Add(Path.GetExtension((sr.ReadLine())))
        ListView.Items.Add(lvi)
    Next

End Sub

But I'm not sure what I'm doing during the "For Each" loop.

+2  A: 

It looks like you're primarily misunderstanding what happens when you use sr.ReadLine(). It's actually a method that "does stuff", not just a property of the next line.

Each time you use this, another line of the response stream is read. Right now you're reading a line:

  1. At the start of the loop
  2. In the constructor of New ListViewItem()
  3. in Path.GetExtension((sr.ReadLine())))

... when it really looks like you're wanting to look at one line at a time here.

I'd suggest using this FTP client as a guide or simply reference it in your project... http://www.codeproject.com/KB/IP/FtpClient.aspx - that way you can work with a model that's a little more intuitive than just a response string.

Separately, though, all you're doing in the ForEach loop is creating a ListViewItem, and adding it to a ListView. There are tons of tutorials to do this (just google How to use a ListView in VB.NET), here's one to get you started:

http://dotnetref.blogspot.com/2007/08/using-listview-in-vbnet.html

Hope this helps.

routeNpingme
Thanx, I'll look into this. But you know, I also have an upload file-sub which connects nicely to a progressbar. Wouldn't it be a bit strange to use FTP Client to just list the items? Or should I use FTP Client in the entire project? Is it better than the built in FTPwebrequest in .Net?
Kenny Bones