Surendra Sharma

Surendra Sharma

Search This Blog

Wednesday, March 19, 2014

How to upload file via secure SFTP using credentials in ASP.NET using C#

In almost all ASP.NET web application, we have to write a code to upload file to secure SFTP servers.

Here is simple and full code to upload file via SFTP.

First you need to download SshNet DLL from

You can connect SFTP by two ways
1.    By using credentials
2.    By using Username and private key

This code is specific to connect to SFTP by using credentials.

It uses SftpClient for creating SFTP connection to server by proving SFTP URL with necessary credentials to any specific folder or root folder. Code read the file and uploads the final stream to SFTP server.

using Renci.SshNet;

public static void UploadFileToSFTPServer(string FilePath, string Address, int Port, string UserName, string Password, string FolderName)
    SftpClient client = new SftpClient(Address, Port, UserName, Password);


    if (!string.IsNullOrEmpty(FolderName))
        client.ChangeDirectory(FolderName + @"/");

    using (var fileStream = new FileStream(completeFilePath, FileMode.Open))
        client.BufferSize = 4 * 1024;
        client.UploadFile(fileStream, Path.GetFileName(completeFilePath), null);

Please leave your comments or share this code if it’s useful for you.

No comments:

Post a Comment