This is an old revision of the document!

Session.ListDirectory Method

Lists the contents of specified remote directory.

This feature is available only in the latest beta release.

Advertisement

Syntax

C#
public RemoteDirectoryInfo ListDirectory(string path)
VB.NET
Public Function ListDirectory(ByVal path As String) As RemoteDirectoryInfo

Parameters

Name Description
string path Full path to remote directory to be read.

Return Value

RemoteDirectoryInfo.

Exceptions

Exception Condition
InvalidOperationException Session is not opened.
SessionLocalException Error communicating with winscp.com.
See the exception documentation for details.
SessionRemoteException Session has failed.
Listing of remote directory has failed.
See the exception documentation for details.
TimeoutException Timeout waiting for winscp.com to respond.

Advertisement

Example

C# Example

using System;
using WinSCP;
 
class Example
{
    public static int Main()
    {
        try
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions {
                Protocol = Protocol.Sftp,
                HostName = "example.com",
                UserName = "user",
                Password = "mypassword",
                SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
            };
 
            using (Session session = new Session())
            {
                // Connect
                session.Open(sessionOptions);
 
                RemoteDirectoryInfo directory = session.ListDirectory("/home/martin/public_html");
 
                foreach (RemoteFileInfo fileInfo in directory.Files)
                {
                    Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}",
                        fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime);
                }
            }
 
            return 0;   
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e);
            return 1;
        }
    }
}

VB.NET Example

Imports System
Imports WinSCP
 
Friend Class Example
 
    Public Shared Function Main() As Integer
 
        Try 
            ' Setup session options
            Dim sessionOptions As New SessionOptions
            With sessionOptions
                .Protocol = Protocol.Sftp,
                .HostName = "example.com",
                .UserName = "user",
                .Password = "mypassword",
                .SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
            End With
 
            Using session As Session = New Session
                ' Connect
                session.Open(sessionOptions)
 
                Dim directory As RemoteDirectoryInfo = session.ListDirectory("/home/martin/public_html")
 
                Dim fileInfo As RemoteFileInfo
                For Each fileInfo In directory.Files
                    Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}", _
                        fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime)
                Next
            End Using
 
            Return 0
        Catch e As Exception
            Console.WriteLine("Error: {0}", e)
            Return 1
        End Try
 
    End Function
 
End Class

Advertisement

Last modified: by martin