Uploading files to an SFTP via C#

To upload files via C# to an SFTP first thing you will need is to install these two nuget packages:

public class FTPService
       private string _ftpUsername;
       private string _ftpPassword;
       private string _ftpAddress;
       private string _remotePath;
       public FTPService()
           _ftpUsername = WebConfigurationManager.AppSettings["FtpUsername"];
           _ftpPassword = WebConfigurationManager.AppSettings["FtpPassword"];
           _ftpAddress = WebConfigurationManager.AppSettings["FtpAddress"];
           _remotePath = WebConfigurationManager.AppSettings["RemotePath"];
       public string UploadImage(string skinPath, Stream file, string uploadFileName, FTPFolderType folderType)
           var value = string.Empty;
               var fileName = Path.GetFileName(uploadFileName);
               var remotePath = string.Format(_remotePath, skinPath, folderType.ToString().ToLower());
               using (var client = new SftpClient(_ftpAddress, _ftpUsername, _ftpPassword))
                   client.UploadFile(file, fileName);
               value = remotePath;
               return value;
           catch (Exception e)
               Debug.WriteLine("Upload of {0} failed with exception {1}", uploadFileName, e);
               throw e;

Leave a Reply

Your email address will not be published. Required fields are marked *