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.

This feature will be available only with the next release.

Advertisement

Three Steps to Start Using WinSCP .NET Assembly

  1. Create an instance of WinSCP.SessionOptions class and fill in all necessary information to allow an automatic connection and authentication of your session.
  2. Create an instance of WinSCP.Session class. Optionally you can hook handlers of some events of the class.
  3. 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, e.g.,

Classes

Namespace: WinSCP

Class Description
ChmodEventArgs Provides data for change of permissions event.
CommandExecutionResult Represents results of Session.ExecuteCommand.
FailedEventArgs Provides data for Session.Failed event.
FileOperationEventArgs Provides data for abstract file operation event.
FilePermissions Represents *nix-style remote file permissions.
OperationEventArgs Provides data for abstract operation event.
OperationResultBase Represents results of abstract batch operation.
OperationResult<T> Represents results of simple batch operation.
RemovalEventArgs 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.
SessionLocalException Exception associated with the Session originating from this assembly.
SessionOptions Defines information to allow an automatic connection and authentication of the session. Is used with Session.Open method.
SessionRemoteException Exception associated with the Session, originating from WinSCP console session.
SynchronizationResult Represents results of synchronization (Session.SynchronizeDirectories).
TouchEventArgs Provides data for remote file timestamp change event.
TransferEventArgs Provides data for file transfer event.
TransferOptions Defines options for file transfers.

Advertisement

Example

using System;
using WinSCP;
 
class Example
{
    public static int 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);
 
                // Upload files
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;
 
                OperationResult<TransferEventArgs> transferResult;
                transferResult = session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);
 
                // Throw on any error
                transferResult.Check();
 
                // Print results
                foreach (TransferEventArgs transfer in transferResult.Operations)
                {
                    Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
                }
            }
 
            return 0;
        }
        catch(Exception e)
        {
            Console.WriteLine("Error: {0}", e);
            return 1;
        }
    }
}

Advertisement

Last modified: by martin