Session.ListDirectory Method

Lists the contents of specified remote directory.

Advertisement

Syntax

C#
public RemoteDirectoryInfo ListDirectory(string path)
VB.NET
Public Function ListDirectory(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

Remarks

You can use Session.EnumerateRemoteFiles method instead, if you want to:

  • List only files matching a wildcard;
  • List the files recursively;
  • Have references to this (.) and parent (..) directories be excluded form the listing.

Examples

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",
                SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
            };
 
            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;
        }
    }
}

Advertisement

VB.NET Example

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"
                .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
            End With
 
            Using session As 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

PowerShell Example

Learn more about using WinSCP .NET assembly from PowerShell.

Advertisement

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "example.com"
        UserName = "user"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        $directory = $session.ListDirectory("/home/martin/public_html")
 
        foreach ($fileInfo in $directory.Files)
        {
            Write-Host ("$($fileInfo.Name) with size $($fileInfo.Length), " +
                "permissions $($fileInfo.FilePermissions) and " +
                "last modification at $($fileInfo.LastWriteTime)")
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

JScript (WSH) Example

In this example the JScript script is embedded into WSF file, to allow access to enumeration values.

Advertisement

<job>                                                               
<reference object="WinSCP.Session"/>
<script language="JScript">
 
try
{
    // Setup session options
    var sessionOptions = WScript.CreateObject("WinSCP.SessionOptions");
    sessionOptions.Protocol = Protocol_Sftp;
    sessionOptions.HostName = "example.com";
    sessionOptions.UserName = "user";
    sessionOptions.Password = "mypassword";
    sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...";
     
    var session = WScript.CreateObject("WinSCP.Session");
 
    try
    {
        // Connect
        session.Open(sessionOptions);
 
        var directoryInfo = session.ListDirectory("/home/martin/public_html")
            
        var enumerator = new Enumerator(directoryInfo.Files);
        for (; !enumerator.atEnd(); enumerator.moveNext())
        {
            var fileInfo = enumerator.item();
            WScript.Echo(
                fileInfo.Name + " with size " + fileInfo.Length +
                ", permissions " + fileInfo.FilePermissions +
                " and last modification at " + fileInfo.LastWriteTime);
        }
    }
    finally
    {
        // Disconnect, clean up
        session.Dispose();
    }
}
catch (e)
{
    WScript.Echo("Error: " + e.message);
    WScript.Quit(1);
}
 
</script>
</job>

VBScript (WSH) Example

In this example the VBScript script is embedded into WSF file, to allow access to enumeration values.

Advertisement

<job>                                                               
<reference object="WinSCP.Session"/>
<script language="VBScript">
 
Option Explicit
 
' Setup session options
Dim sessionOptions
Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions")
With sessionOptions
    .Protocol = Protocol_Sftp
    .HostName = "example.com"
    .UserName = "user"
    .Password = "mypassword"
    .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
End With
 
Dim session
Set session = WScript.CreateObject("WinSCP.Session")
 
' Connect
session.Open sessionOptions
 
Dim directoryInfo
Set directoryInfo = session.ListDirectory("/home/martin/public_html")
 
Dim fileInfo
For Each fileInfo In directoryInfo.Files
    WScript.Echo fileInfo.Name & " with size " & fileInfo.Length & _
        ", permissions " & fileInfo.FilePermissions & _
        " and last modification at " & fileInfo.LastWriteTime
Next
 
' Disconnect, clean up
session.Dispose
 
</script>
</job>

Real-Life Examples

Last modified: by martin