This is an old revision of the document!

WinSCP .NET Assembly

WinSCP .NET Assembly winscp.dll is C# wrapper around WinSCP scripting interface that allows connecting to a remote machine and manipulating remote files over SFTP, SCP or FTP session from .NET applications.

Advertisement

Using WinSCP .NET Assembly

First, you need to create an instance of WinSCP.SessionOptions class and fill in all necessary information to allow an automatic connection and authentication of your session.

Then you create an instance of WinSCP.Session class. Optionally you hook handlers of some events of the class. Then you open the session using Session.Open method, passing instance of your WinSCP.SessionOptions.

Once the session is opened, you can use any of the WinSCP.Session methods to manipulate remote files. For example use Session.GetFiles to download files, Session.PutFiles to upload files or Session.SynchronizeDirectories to synchronize directories.

Classes

Namespace: WinSCP

Class Description
ChmodArgs Provides data for change of permissions event.
FailedArgs Provides data for Session.Failed event.
OperationResultBase Represents results of abstract batch operation.
OperationResult<T> Represents results of simple batch operation.
Permissions Represents *nix-style remote file permissions.
RemovalArgs Provides data for remote file removal event.
RemoteFileInfo Represents data about remote file.
Session Represents session. Provides methods for manipulating remote files.
SessionException Exception associated with the Session.
SessionOptions Defines information to allow an automatic connection and authentication of the session. Is used with Session.Open method.
SynchronizationResult Represents results of synchronization (Session.SynchronizeDirectories).
TouchArgs Provides data for remote file timestamp change event.
TransferArgs Provides data for Session.FileTransfer event.
TransferOptions Defines options for file transfers.

Advertisement

Example

using WinSCP;
 
class Test
{
    static void Main()
    {
        try
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions();
            sessionOptions.Protocol = SessionProtocols.Sftp;
            sessionOptions.HostName = "example.com";
            sessionOptions.UserName = "user";
            sessionOptions.Password = "mypassword";
            sessionOptions.HostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx";
 
            // Connect
            Session session = new Session();
            session.Open(sessionOptions);
 
            // Upload files
            TransferOptions transferOptions = new TransferOptions();
            transferOptions.TransferMode = TransferMode.Binary;
 
            OperationResult<TransferArgs> transferResult;
            transferResult = session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);
 
            // Throw on any error
            transferResult.Check();
 
            // Print results
            foreach (TransferArgs transfer in transferResult)
            {
                if (transfer.Error == null)
                {
                    Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
                }
                else
                {
                    Console.WriteLine("Upload of {0} failed: {1}", transfer.FileName, transfer.Error);
                }
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Error: {0}", e);
        }
    }
}

Advertisement

Last modified: by martin