SFTP stands for SSH File Transfer Protocol. It provides file access, file management, and file transfer over a data stream. With the Xceed's SFTP component, that data stream is an SSH connection.
SSH stands for Secure Shell. It is a network protocol for secure communication between two networked computers: a server and a client. Xceed's SFTP component implements the client side of SSH and SFTP.
Xceed SFTP is compiled for .NET, Xamarin Android and Xamarin iOS. The usage is the same for all platforms except for the license key.
In order to use SFTP, an SSH connection must first be established. The SSH connection handles the network communication, setting up key exchange, encryption, data integrity and authentication, and disconnecting. The Xceed.SSH.Client namespace implements the SSHClient class, which exposes methods and properties for these actions.
An SSHClient object is typically setup like this:
SSHClient implements the System.IDisposable interface. As such, it can be used as part of a using block to make sure the client gets disconnected and resources cleaned-up:
Main article: Proxy Support
Xceed SFtp for .NET is a proxy client. It supports HTTP and SOCKS proxy protocols to allow a proxy server to establish a connection to a SSH server on its behalf.
Main article: Key exchange and algorithm negotiation
A SSH connection implies the use of several algorithms that, together, make the connection secure. There are several encryption, data integrity, key exchange, public key and compression algorithms to choose from. These choices are made between the client (Xceed SFtp for .NET) and the server during a phase called key exchange.
Main article: Public Key Authentication
Main article: Keyboard-Interactive Authentication
The examples above uses password authentication. The component also supports public key authentication and keyboard-interactive authentication.
Once the SSH connection has been successfully established, SFTP sessions can be started. The Xceed.SSH.Client namespace implements the SFtpSession class. This class represents a session between a client and an SFTP server. The SFtpSession class implements the %IDisposable:System.IDisposable% interface, meaning that every SFtpSession object that is created should also be disposed of by calling the Dispose method or, in C#, creating the SFtpSession object within a using block. If an instance of an SFtpSession object is not disposed of, its connection with the SFtp server may remain active until the SFtp server times-out or the garbage collector clears the object.
An SFtpSession object is typically setup like this:
The SFtpFolder class is a specialization of the AbstractFolder class that exposes properties and methods that provide access to a folder located on an SFTP server.
The SFtpFile class is a specialization of the AbstractFile class that exposes properties and methods that provide access to a file located on an SFTP server.
The SFtpFolder and SFtpFile classes allow you to list folder contents, send files, receive files and get file and folder information.
Items can be copied or moved (in other words, downloaded) from an SFTP server to a local drive, or any other type of folder supported by the Xceed FileSystem, using the CopyTo, CopyFilesTo, MoveTo, or MoveFilesTo methods.
Items can be copied or moved (in other words, uploaded) from a local drive, or any other type of folder supported by the Xceed FileSystem, to an SFTP server using the CopyTo, CopyFilesTo, MoveTo, or MoveFilesTo methods.
To download from a server, select a remote file for download, select a local folder as the destination and call the copy or move methods using the remote file as the source and the local folder as the destination.
The destination doesn't have to be a folder. You can specify a custom filename when selecting the destination. That is possible because FileSystemItem objects (and their derived objects and don't need to represent physical entities that already exist.
The contents of the remote file will be copied over the local file that bears the specified name.
A group of files can be downloaded from a folder with the CopyFilesTo() and MoveFilesTo(). A filter system is available to select files. By default, all files from the source folder are selected. In fact, when all files need to be selected for an operation, it's more efficient to not specify any filter than to use a "*" or "*.*" filter.
The FileSystem filter system allows you to select a specific group of files to be selected based on a criteria.
A folder itself, along with all its contents (no filtering applied), can be downloaded with the CopyTo() and MoveTo() methods.
To upload from a local drive to a location on a server, the CopyTo/MoveTo methods are used. A local file is selected as the source and a remote folder is selected as the destination.
The remote destination doesn't have to be a folder. You can specify a custom filename when selecting the destination. That is possible because FileSystemItem objects (and their derived objects and don't need to represent physical entities that already exist.
The contents of the local file will be copied over the remote file that bears the specified name.
If the situation requires it, it is possible to perform a manual upload by implementing the copy loop with an appropriate buffer size.
Main article: Performing a manual upload
A group of files can be uploaded from a folder with the CopyFilesTo() and MoveFilesTo(). A filter system is available to select files. By default, all files from the source folder are selected. In fact, when all files need to be selected for an operation, it's more efficient to not specify any filter than to use a "*" or "*.*" filter.
The FileSystem filter system allows you to select a specific group of files to be selected based on a criteria.
A folder itself, along with all its contents (no filtering applied), can be uploaded with the CopyTo() and MoveTo() methods.
The contents of folders can be obtained with the GetItems() method. It returns an array of FileSystemItem objects that will be objects derived from AbstractFile or AbstractFolder. The method takes a recursive parameter that specifies if the contents of sub folders are to be included in the list.
The GetFolders() method works on the same principle as GetItems() but it only returns AbstractFolder objects.
The GetFiles() method works on the same principle as GetItems() but it only returns AbstractFiles objects.
The GetFiles() method accepts filters as parameters to narrow down the list of files returned.
A SFtpFile, like any AbstractFile, can be renamed simply by setting its Name property to a new value.
Renaming a folder works the same way as renaming a file, the SFtpFolder's Name property value is changed.
The Delete() method deletes files. The method throws an exception if the file to delete doesn't exist. It is good practice to check if the file exists before calling Delete().
The Delete() method deletes folders. All the files and sub folders will be deleted from the selected folder. Then, the folder itself will be deleted. The method throws an exception if the folder to delete doesn't exist. It is good practice to check if the folder exists before calling Delete().
The Create() method creates files. The method throws an exception if the file to create already exists.
The Create() method also creates folders. The method throws an exception if the folder to create already exists.
SFTP defines the directory path separator as '/'. However, the Xceed FileSystem uses the '\' character. When using SFtpFolder and SftpFile, always use '\' as the folder separator. This allows consistency with the other specializations of FileSystemItem (DiskFile, ZippedFolder, etc). The component will translate the separator internally.
SFTP has concepts of the default directory, absolute and relative directories. File names starting with a backslash ('\') are "absolute", and are relative to the root of the file system. That root is defined by the server and its value usually depends on the user that is authenticated. Names starting with any other character are relative to the user's default directory. That default directory is defined by the server and its value usually depends on the user that is authenticated. An empty path name is valid, and refers to the default directory. A path name component ".." refers to the parent directory, and "." refers to the current directory.
These conventions are in effect when you specify paths for SFtpFile and SFtpFolder.
For example, the following lines both make 'destinationFolder' refer to the default directory. SFtpFolder internally resolves the logical default directory so that you can get the full, absolute path if you get the value of its FullName property. You will also get the folder name that the default path refers to with the Name property.The following line refers to a file, relative to the default directory. SFtpFile internally resolves the relative path so that you can get the full, absolute path if you get the value of its FullName property.
The SFTP server decides what files and folders you see and files you have access to. Most of the time, access depends on the authenticated SSH user.
The SSH protocol has the ability to multiplex a single connection into several logical channels. SFTP is one such channel. That means that you can use the same SSHClient object for multiple SFtpSession objects. For example, the following is allowed:
Understand that creating multiple sessions will not necessarily increase overall transmission speed.
This component implements version 2 of the SSH protocols (usually called SSH-2). Based on RFC 4250, RFC 4251, RFC 4252, RFC 4253, RFC 4254 and draft-ietf-secsh-filexfer-13. SSH-1 is not supported and is not planned.
The component uses the following client version string:
SSH-2.0-Xceed.SSH.<version number>
where <version number> is the assembly's major and minor version number (for example, SSH-2.0-Xceed.SSH.5.2).
The component implements the following algorithms. The order in which the algorithms are listed in each category indicates the default preference of the component when negociating a connection. During negotiation, the client and the server exchange lists of supported algorithms. The first common algorithms between the lists are then used for the connection. So the higher the position in the list, the more an algorithm has a chance of being used.
Support for more algorithms will be added in future versions of the component.
The component implements portions of version 6 of the SFTP protocol. It also supports version 3. The component asks the server for version 6 but accepts version 3 if that version is included in the server's reply. The component does not currently support any other version number (below 3 and version 4 and 5).
The component has been tested with WinSSHD and OpenSSH server software.