16 October 2015

FTP/SFTP in .NET using WSFTP .NET SDK

Sometimes we have requirement of connecting with a FTP and SFTP server using .NET code and download/upload files from/to the server. Some of the basic requirements can be:-

  • Connect to FTP/SFTP server
  • Download specific files from the server
  • Upload specific files to the server
  • Disconnect from server
There are no built-in features in .NET to perform such a task. Hence we can use .NET SDK provided by WSFTP to achieve our requirement. This SDK enables secure file transfer (SFTP) technology to be embedded in applications using .NET. The SDK implements the basic algorithms for FTP/SFTP and provides us simple classes and methods to use.

This SDK is licensed software and if it needs to be used, we need to purchase it from WSFTP. The details about the WSFTP .NET SDK can be found at WSFTP official site.

WSFTP .NET SDK is basically a DLL which provides basic classes and methods for performing basic FTP functions like connecting, get-files, put-files etc.

We needed to create our own wrapper around it and add our own custom .NET code to utilize it.

Implementation

We need to first add the WsftpSDK.dll in our project. Visual Studio will create its Interop DLL automatically.
Then we can write a class which can act as a wrapper around the WSFTP DLL. The class can have following basic functions:-

  • ConnectToSFTP
  • DisconnectFromSFTP
  • DownloadFile
  • UploadFile
Sample Code

Connect to SFTP

/// <summary>
/// Method to connect to FTP/SFTP server
/// </summary>
/// <returns></returns>
public WsftpSDK.WsftpConnection ConnectToSFTP(string host,
                                                string hostType,
                                                string userId,
                                                string password,
                                                int port)
{
    WsftpSDK.WsftpApiClass ftpApi;
    WsftpSDK.WsftpConnection conn;

    ftpApi = new WsftpSDK.WsftpApiClass();

    //connect to FTP/SFTP
    conn = ftpApi.CreateConnection(host,
                                    userId,
                                    password,
                                    GetHostType(hostType),
                                    port);

    if (conn == null)
    {
        //This implies that not able to connect to SFTP

        //Get error message
        string errorMsg = ftpApi.GetErrorString(ftpApi.LastError);

        //Log errorMsg in DB or show on screen
    }

    return conn;
}

/// <summary>
/// Method to get int host type from string host type
/// </summary>
private int GetHostType(string hostType)
{
    WsftpSDK.WsftpConnectionTypes connectionType = WsftpSDK.WsftpConnectionTypes.WSFTP_CT_FTP;

    switch (hostType)
    {
        case "SFTP":
            connectionType = WsftpSDK.WsftpConnectionTypes.WSFTP_CT_SFTP;
            break;
        case "FTP":
            connectionType = WsftpSDK.WsftpConnectionTypes.WSFTP_CT_FTP;
            break;
        case "FTPS":
            connectionType = WsftpSDK.WsftpConnectionTypes.WSFTP_CT_FTPS;
            break;
        case "FTPAUTHSSL":
            connectionType = WsftpSDK.WsftpConnectionTypes.WSFTP_CT_FTPAUTHSSL;
            break;
    }

    return Convert.ToInt32(connectionType);
}



Disconnect from SFTP

/// <summary>
/// Method to disconnect from (S)FTP server
/// </summary>
/// <param name="conn"></param>
public void DisconnectFromSFTP(WsftpSDK.WsftpConnection conn)
{
    if (conn != null)
    {
        conn.Disconnect();
    }
}


Download file

public bool DownloadFile(WsftpSDK.WsftpConnection conn,
                            string sourceFolder,
                            string fileName,
                            string destinationFolder,
                            string destinationFileName)
{

    //copy files from SFTP server to local folder
    bool fileCopyStatus = conn.GetFile(sourceFolder,
                                        fileName,
                                        destinationFolder,
                                        destinationFileName,
                                        (int)WsftpSDK.WsftpTransferModes.WSFTP_TFF_BINARY,
                                        0);

    return fileCopyStatus;
}

Upload file

public bool UploadFile(WsftpSDK.WsftpConnection conn,
                        string sourceFolder,
                        string sourcefileName,
                        string destinationFolder,
                        string destinationFileName)
{

    //copy file from local folder to (S)FTP folder
    bool fileCopyStatus = conn.PutFile(sourceFolder,
                                        sourcefileName,
                                        destinationFolder,
                                        destinationFileName,
                                        (int)WsftpSDK.WsftpTransferModes.WSFTP_TFF_BINARY,
                                        0);

    return fileCopyStatus;
}

No comments: