This is an old revision of the document!

Session.ListDirectory Method

Lists the contents of specified remote directory.

This feature will be available only with the next release.

Advertisement

Syntax

public Collection<RemoteFileInfo> ListDirectory(string path)

Parameters

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

Return Value

Collection of RemoteFileInfo.

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

using System;
using System.Collections.ObjectModel;
using WinSCP;
 
class Test
{
    static void Main()
    {
        try
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions();
            sessionOptions.Protocol = Protocol.Sftp;
            sessionOptions.HostName = "example.com";
            sessionOptions.UserName = "user";
            sessionOptions.Password = "mypassword";
            sessionOptions.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);
 
                Collection<RemoteFileInfo> files = session.ListDirectory("/home/martin/public_html");
 
                foreach (RemoteFileInfo fileInfo in files)
                {
                    Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}",
                        fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e);
        }
    }
}

Last modified: by martin