In this tutorial, I will describe a technique for uploading a file to sftp server using third party tools WinSCP. WinSCP is an open source free SFTP client, FTP client, WebDAV client and SCP client for Windows. Its main function is file transfer between a local and a remote computer. Beyond this, WinSCP offers scripting and basic file manager functionality.

Features

  • Graphical user interface
  • Translated into many languages
  • Integration with Windows (drag&drop, URL, shortcut icons, jump list)
  • All common operations with files
  • Support for SFTP and SCP protocols over SSH and FTP and WebDAV protocols
  • Batch file scripting and command-line interface and .NET assembly for advanced programming tasks
  • Directory synchronization in several semi or fully automatic ways
  • Integrated text editor
  • Shares site settings with PuTTY
  • Support for password, keyboard-interactive, public key and Kerberos (GSS) authentication
  • Integrates with Pageant (PuTTY authentication agent) for full support of public key authentication with SSH
  • Explorer and Commander interfaces
  • Optionally protects stored site information with master password
  • Optionally supports portable operation using a configuration file in place of registry entries, suitable for operation from removable media

Now, write the following code:
private static void UploadToSFTP(string stringJasontext) 

try 

    //get static value from App.config file. 
    string ftpServerIP = ConfigurationSettings.AppSettings["sftpServerIP"].ToString(); 
    string stringsFtpUserID = ConfigurationSettings.AppSettings["sftpUserID"].ToString(); 
    string stringsFtpPassword = ConfigurationSettings.AppSettings["sftpPassword"].ToString(); 
    string stringStrDate = System.DateTime.Now.ToString("dd_MM_yyyy-hh_mm_ss"); 
    string stringFileName = "LeaseJson_" + stringStrDate + ".json"; 
    string stringFromPath = ConfigurationSettings.AppSettings["sFromPath"].ToString(); 
    string stringToPath = ConfigurationSettings.AppSettings["sToPath"].ToString(); 
    string stringHostKey = ConfigurationSettings.AppSettings["sHostKey"].ToString(); 
    string stringsBackUpFolder = "Processed"; 
    //create folder for back up data 
    if (!Directory.Exists(stringFromPath + stringsBackUpFolder)) 
    { 
          Directory.CreateDirectory(stringFromPath + stringsBackUpFolder); 
    } 
    //check whether file exist or not in local machine. 
    if (!System.IO.File.Exists(stringFromPath + stringFileName)) 
    { 
          using (FileStream fileStreamLocalFile = File.Create(stringFromPath + stringFileName)) 
          { 
                byte[] byteLocalFile = new UTF8Encoding(true).GetBytes(stringJasontext); 
                fileStreamLocalFile.Write(byteLocalFile, 0, byteLocalFile.Length); 
          } 
    } 
    SessionOptions sessionOptionsSFTP = new SessionOptions 
    { 
          Protocol = Protocol.Sftp, 
          HostName = ftpServerIP, 
          UserName = stringsFtpUserID, 
          Password = stringsFtpPassword, 
          PortNumber = 22, 
          SshHostKeyFingerprint = stringHostKey 
    }; 
    WinSCP.Session sessionSFTP = new WinSCP.Session(); 
    sessionSFTP.Open(sessionOptionsSFTP); 
    TransferOptions transferOptionsSFTP = new TransferOptions(); 
    transferOptionsSFTP.TransferMode = TransferMode.Binary; 
    transferOptionsSFTP.FilePermissions = null; 
    transferOptionsSFTP.PreserveTimestamp = false; 
    transferOptionsSFTP.ResumeSupport.State = TransferResumeSupportState.Off; 
    TransferOperationResult transferOperationResultSFTP; 
    transferOperationResultSFTP = sessionSFTP.PutFiles(stringFromPath + stringFileName, stringToPath, false, transferOptionsSFTP); 
    if (System.IO.File.Exists(stringFromPath + stringFileName)) 
    { 
          File.Move(stringFromPath + stringFileName, stringFromPath + "\\" + stringsBackUpFolder + "\\" + stringFileName); 
    } 
    transferOperationResultSFTP.Check();

    if (transferOperationResultSFTP.IsSuccess == true) 
    { 
          Console.Write("File upload successfully"); 
    } 
    else 
    { 
          Console.Write("File upload failed"); 
    } 


catch (Exception exError) 

    Console.Write(exError.Message); 


#endregion 
#region Upload File to FTP server as Json File 
//private static void UploadToFTP(string stringJasontext) 
//{ 
// try 
// { 
// string stringFtpUserID = ConfigurationSettings.AppSettings["ftpUserID"].ToString(); 
// string stringFtpPassword = ConfigurationSettings.AppSettings["ftpPassword"].ToString(); 
// string stringStrDate = System.DateTime.Now.ToString("dd_MM_yyyy-hh_mm"); 
// string stringToPath = ConfigurationSettings.AppSettings["ToPath"].ToString() + "LeaseJson_" + stringStrDate + ".json"; 
// FtpWebRequest ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(stringToPath); 
// ftpWebRequest.Credentials = new NetworkCredential(stringFtpUserID, stringFtpPassword); 
// ftpWebRequest.KeepAlive = false; 
// ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile; 
// ftpWebRequest.UseBinary = true; 
// ftpWebRequest.ContentLength = stringJasontext.Length; 
// ftpWebRequest.Proxy = null; 
// using (Stream TempStream = ftpWebRequest.GetRequestStream()) 
// { 
// System.Text.ASCIIEncoding TempEncoding = new System.Text.ASCIIEncoding(); 
// byte[] byteTempBytes = TempEncoding.GetBytes(stringJasontext); 
// TempStream.Write(byteTempBytes, 0, byteTempBytes.Length); 
// } 
// ftpWebRequest.GetResponse(); 
// } 
// catch (Exception exError) 
// { 
// Console.Write(exError.Message); 
// } 
//} 
#endregion 

 

HostForLIFE.eu ASP.NET Core 1.0 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.