SFTP file list (.NET Compact Framework)

Demonstrates the power of GetList method and SftpItemCollection class.

A utility that connects to a given SFTP server and displays file list for a given directory (both VB.NET and C#).

C#

// select the desired directory
sftp.ChangeDirectory(path);

// retrieve the list of files and directories
SftpItemCollection list = sftp.GetList();

// display retrieved items
listView.BeginUpdate();

foreach (SftpItem item in list)
{
    ListViewItem listViewItem = new ListViewItem(item.Name);
    listViewItem.SubItems.Add(item.Length.ToString());
    listView.Items.Add(listViewItem);
}

listView.EndUpdate();

VB.NET

' select the desired directory
sftp.ChangeDirectory(path)

' retrieve the list of files and directories
Dim list As SftpItemCollection = sftp.GetList()

' display retrieved items
listView.BeginUpdate()

Dim item As SftpItem
For Each item In list
    Dim listViewItem As ListViewItem = New ListViewItem(item.Name)
    listViewItem.SubItems.Add(item.Length.ToString())
    listView.Items.Add(listViewItem)
Next item

listView.EndUpdate()

It is that simple even in .NET Compact Framework!