File Transfer Between Windows And Linux Using WinSCP In C#

Introduction

 
In this article, we will learn how to copy files from Windows to Linux and download files from Linux to Windows. Here I am using Visual Studio 2019 and .NetFrameWork 4.7 and WinSCP. WinSCP .NET assembly winscp.dll is a .NET wrapper around WinSCP’s scripting interface that allows your code to connect to a remote machine and manipulate remote files over SFTP, SCP, and FTP sessions from .NET languages, such as C#, VB.NET, PowerShell, etc. Here I am using SFTP. SFTP is an interactive program that lets us upload and download files between a Windows and Linux Server.
 
For this operation, we need the following details of the Linux server.
  • HostName :
  • UserName :
  • Password :
  • SshHostKeyFingerprint :
  • SshHostKeyFingerprint looks like
    ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx

    or

    ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
Step 1
  1. Open Visual Studio 2019 click on create a new project.
  2. Select Windows Form App from templates and click on “Next”.
  3. Then give the project name as “FileTransfer” and then click “Create”.
Before coding add WinSCP reference to the project.
 

Creating Master Form and Child Forms

 
Step 2
 
Rename the form “Form1” to “FilesTransfer” and set “IsMdiContainer” property of the form to true and create two new forms, “FileUpload” and “FileDownload”.
 
The FilesTransfer form will look like the below image,
 
File Transfer Between Windows And Linux Using WinSCP In C# 
 
Code for FilesTransfer.Designer.cs
  1. namespace FileTransfer  
  2. {  
  3.     partial class FilesTransfer  
  4.     {  
  5.         /// <summary>  
  6.         /// Required designer variable.  
  7.         /// </summary>  
  8.         private System.ComponentModel.IContainer components = null;  
  9.   
  10.         /// <summary>  
  11.         /// Clean up any resources being used.  
  12.         /// </summary>  
  13.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>  
  14.         protected override void Dispose(bool disposing)  
  15.         {  
  16.             if (disposing && (components != null))  
  17.             {  
  18.                 components.Dispose();  
  19.             }  
  20.             base.Dispose(disposing);  
  21.         }  
  22.  
  23.         #region Windows Form Designer generated code  
  24.   
  25.         /// <summary>  
  26.         /// Required method for Designer support - do not modify  
  27.         /// the contents of this method with the code editor.  
  28.         /// </summary>  
  29.         private void InitializeComponent()  
  30.         {  
  31.             this.menuStrip1 = new System.Windows.Forms.MenuStrip();  
  32.             this.filesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();  
  33.             this.uploadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();  
  34.             this.downloadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();  
  35.             this.menuStrip1.SuspendLayout();  
  36.             this.SuspendLayout();  
  37.             //   
  38.             // menuStrip1  
  39.             //   
  40.             this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {  
  41.             this.filesToolStripMenuItem});  
  42.             this.menuStrip1.Location = new System.Drawing.Point(0, 0);  
  43.             this.menuStrip1.Name = "menuStrip1";  
  44.             this.menuStrip1.Size = new System.Drawing.Size(684, 24);  
  45.             this.menuStrip1.TabIndex = 1;  
  46.             this.menuStrip1.Text = "menuStrip1";  
  47.             //   
  48.             // filesToolStripMenuItem  
  49.             //   
  50.             this.filesToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {  
  51.             this.uploadToolStripMenuItem,  
  52.             this.downloadToolStripMenuItem});  
  53.             this.filesToolStripMenuItem.Name = "filesToolStripMenuItem";  
  54.             this.filesToolStripMenuItem.Size = new System.Drawing.Size(42, 20);  
  55.             this.filesToolStripMenuItem.Text = "Files";  
  56.             //   
  57.             // uploadToolStripMenuItem  
  58.             //   
  59.             this.uploadToolStripMenuItem.Name = "uploadToolStripMenuItem";  
  60.             this.uploadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);  
  61.             this.uploadToolStripMenuItem.Text = "Upload";  
  62.             this.uploadToolStripMenuItem.Click += new System.EventHandler(this.UploadToolStripMenuItem_Click);  
  63.             //   
  64.             // downloadToolStripMenuItem  
  65.             //   
  66.             this.downloadToolStripMenuItem.Name = "downloadToolStripMenuItem";  
  67.             this.downloadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);  
  68.             this.downloadToolStripMenuItem.Text = "Download";  
  69.             this.downloadToolStripMenuItem.Click += new System.EventHandler(this.DownloadToolStripMenuItem_Click);  
  70.             //   
  71.             // FilesTransfer  
  72.             //   
  73.             this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);  
  74.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  75.             this.ClientSize = new System.Drawing.Size(684, 490);  
  76.             this.Controls.Add(this.menuStrip1);  
  77.             this.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));  
  78.             this.IsMdiContainer = true;  
  79.             this.MainMenuStrip = this.menuStrip1;  
  80.             this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);  
  81.             this.MaximizeBox = false;  
  82.             this.Name = "FilesTransfer";  
  83.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;  
  84.             this.Text = "SFTP Files Transfer";  
  85.             this.Load += new System.EventHandler(this.FilesTransfer_Load);  
  86.             this.menuStrip1.ResumeLayout(false);  
  87.             this.menuStrip1.PerformLayout();  
  88.             this.ResumeLayout(false);  
  89.             this.PerformLayout();  
  90.   
  91.         }  
  92.  
  93.         #endregion  
  94.   
  95.         private System.Windows.Forms.MenuStrip menuStrip1;  
  96.         private System.Windows.Forms.ToolStripMenuItem filesToolStripMenuItem;  
  97.         private System.Windows.Forms.ToolStripMenuItem uploadToolStripMenuItem;  
  98.         private System.Windows.Forms.ToolStripMenuItem downloadToolStripMenuItem;  
  99.     }  
  100. }  
Code for FilesTransfer.cs
  1. using System;  
  2. using System.Windows.Forms;  
  3.   
  4. namespace FileTransfer  
  5. {  
  6.     public partial class FilesTransfer : Form  
  7.     {  
  8.         FileUpload FU = null;  
  9.         FileDownload FD = null;  
  10.         public FilesTransfer()  
  11.         {  
  12.             InitializeComponent();  
  13.         }  
  14.   
  15.         private void UploadToolStripMenuItem_Click(object sender, EventArgs e)  
  16.         {  
  17.             if (FU == null)  
  18.             {  
  19.                 FU = new FileUpload();  
  20.                 FU.ShowDialog();  
  21.             }  
  22.             else if (FU.Visible == true)  
  23.             {  
  24.                 FU.WindowState = FormWindowState.Normal;  
  25.                 FU.ShowDialog();  
  26.                 FU.Focus();  
  27.             }  
  28.             else  
  29.             {  
  30.                 FU = new FileUpload();  
  31.                 FU.ShowDialog();  
  32.             }  
  33.         }  
  34.   
  35.         private void DownloadToolStripMenuItem_Click(object sender, EventArgs e)  
  36.         {  
  37.             if (FD == null)  
  38.             {  
  39.                 FD = new FileDownload();  
  40.                 FD.ShowDialog();  
  41.             }  
  42.             else if (FD.Visible == true)  
  43.             {  
  44.                 FD.WindowState = FormWindowState.Normal;  
  45.                 FD.ShowDialog();  
  46.                 FD.Focus();  
  47.             }  
  48.             else  
  49.             {  
  50.                 FD = new FileDownload();  
  51.                 FD.ShowDialog();  
  52.             }  
  53.         }  
  54.     }  
  55. }  
FileUpload form.
 
Step 3
 
By using “FileUpload” form we can upload files from Windows to Linux Server.
 
Code for FileUpload.Designer.cs
  1. namespace FileTransfer  
  2. {  
  3.     partial class FileUpload  
  4.     {  
  5.         /// <summary>  
  6.         /// Required designer variable.  
  7.         /// </summary>  
  8.         private System.ComponentModel.IContainer components = null;  
  9.   
  10.         /// <summary>  
  11.         /// Clean up any resources being used.  
  12.         /// </summary>  
  13.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>  
  14.         protected override void Dispose(bool disposing)  
  15.         {  
  16.             if (disposing && (components != null))  
  17.             {  
  18.                 components.Dispose();  
  19.             }  
  20.             base.Dispose(disposing);  
  21.         }  
  22.  
  23.         #region Windows Form Designer generated code  
  24.   
  25.         /// <summary>  
  26.         /// Required method for Designer support - do not modify  
  27.         /// the contents of this method with the code editor.  
  28.         /// </summary>  
  29.         private void InitializeComponent()  
  30.         {  
  31.             this.label1 = new System.Windows.Forms.Label();  
  32.             this.label2 = new System.Windows.Forms.Label();  
  33.             this.label3 = new System.Windows.Forms.Label();  
  34.             this.label4 = new System.Windows.Forms.Label();  
  35.             this.label5 = new System.Windows.Forms.Label();  
  36.             this.label6 = new System.Windows.Forms.Label();  
  37.             this.lblStatus = new System.Windows.Forms.Label();  
  38.             this.btnUpload = new System.Windows.Forms.Button();  
  39.             this.btnClose = new System.Windows.Forms.Button();  
  40.             this.txtHostName = new System.Windows.Forms.TextBox();  
  41.             this.txtUserName = new System.Windows.Forms.TextBox();  
  42.             this.txtPassword = new System.Windows.Forms.TextBox();  
  43.             this.txtHostKey = new System.Windows.Forms.TextBox();  
  44.             this.txtSourcePath = new System.Windows.Forms.TextBox();  
  45.             this.txtDestinationPath = new System.Windows.Forms.TextBox();  
  46.             this.SuspendLayout();  
  47.             //   
  48.             // label1  
  49.             //   
  50.             this.label1.AutoSize = true;  
  51.             this.label1.Location = new System.Drawing.Point(25, 23);  
  52.             this.label1.Name = "label1";  
  53.             this.label1.Size = new System.Drawing.Size(74, 17);  
  54.             this.label1.TabIndex = 0;  
  55.             this.label1.Text = "Host Name";  
  56.             //   
  57.             // label2  
  58.             //   
  59.             this.label2.AutoSize = true;  
  60.             this.label2.Location = new System.Drawing.Point(25, 56);  
  61.             this.label2.Name = "label2";  
  62.             this.label2.Size = new System.Drawing.Size(74, 17);  
  63.             this.label2.TabIndex = 1;  
  64.             this.label2.Text = "User Name";  
  65.             //   
  66.             // label3  
  67.             //   
  68.             this.label3.AutoSize = true;  
  69.             this.label3.Location = new System.Drawing.Point(25, 88);  
  70.             this.label3.Name = "label3";  
  71.             this.label3.Size = new System.Drawing.Size(64, 17);  
  72.             this.label3.TabIndex = 2;  
  73.             this.label3.Text = "Password";  
  74.             //   
  75.             // label4  
  76.             //   
  77.             this.label4.AutoSize = true;  
  78.             this.label4.Location = new System.Drawing.Point(25, 119);  
  79.             this.label4.Name = "label4";  
  80.             this.label4.Size = new System.Drawing.Size(127, 17);  
  81.             this.label4.TabIndex = 3;  
  82.             this.label4.Text = "Host Key Fingerprint";  
  83.             //   
  84.             // label5  
  85.             //   
  86.             this.label5.AutoSize = true;  
  87.             this.label5.Location = new System.Drawing.Point(25, 154);  
  88.             this.label5.Name = "label5";  
  89.             this.label5.Size = new System.Drawing.Size(77, 17);  
  90.             this.label5.TabIndex = 4;  
  91.             this.label5.Text = "Source Path";  
  92.             //   
  93.             // label6  
  94.             //   
  95.             this.label6.AutoSize = true;  
  96.             this.label6.Location = new System.Drawing.Point(25, 191);  
  97.             this.label6.Name = "label6";  
  98.             this.label6.Size = new System.Drawing.Size(102, 17);  
  99.             this.label6.TabIndex = 5;  
  100.             this.label6.Text = "Destination Path";  
  101.             //   
  102.             // lblStatus  
  103.             //   
  104.             this.lblStatus.Location = new System.Drawing.Point(25, 231);  
  105.             this.lblStatus.Name = "lblStatus";  
  106.             this.lblStatus.Size = new System.Drawing.Size(435, 25);  
  107.             this.lblStatus.TabIndex = 6;  
  108.             this.lblStatus.Text = "Status :";  
  109.             this.lblStatus.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;  
  110.             //   
  111.             // btnUpload  
  112.             //   
  113.             this.btnUpload.Location = new System.Drawing.Point(300, 292);  
  114.             this.btnUpload.Name = "btnUpload";  
  115.             this.btnUpload.Size = new System.Drawing.Size(75, 31);  
  116.             this.btnUpload.TabIndex = 7;  
  117.             this.btnUpload.Text = "Upload";  
  118.             this.btnUpload.UseVisualStyleBackColor = true;  
  119.             this.btnUpload.Click += new System.EventHandler(this.BtnUpload_Click);  
  120.             //   
  121.             // btnClose  
  122.             //   
  123.             this.btnClose.Location = new System.Drawing.Point(381, 292);  
  124.             this.btnClose.Name = "btnClose";  
  125.             this.btnClose.Size = new System.Drawing.Size(75, 31);  
  126.             this.btnClose.TabIndex = 8;  
  127.             this.btnClose.Text = "Close";  
  128.             this.btnClose.UseVisualStyleBackColor = true;  
  129.             this.btnClose.Click += new System.EventHandler(this.BtnClose_Click);  
  130.             //   
  131.             // txtHostName  
  132.             //   
  133.             this.txtHostName.Location = new System.Drawing.Point(156, 20);  
  134.             this.txtHostName.Name = "txtHostName";  
  135.             this.txtHostName.Size = new System.Drawing.Size(304, 25);  
  136.             this.txtHostName.TabIndex = 9;  
  137.             //   
  138.             // txtUserName  
  139.             //   
  140.             this.txtUserName.Location = new System.Drawing.Point(156, 53);  
  141.             this.txtUserName.Name = "txtUserName";  
  142.             this.txtUserName.Size = new System.Drawing.Size(304, 25);  
  143.             this.txtUserName.TabIndex = 10;  
  144.             //   
  145.             // txtPassword  
  146.             //   
  147.             this.txtPassword.Location = new System.Drawing.Point(156, 85);  
  148.             this.txtPassword.Name = "txtPassword";  
  149.             this.txtPassword.PasswordChar = '*';  
  150.             this.txtPassword.Size = new System.Drawing.Size(304, 25);  
  151.             this.txtPassword.TabIndex = 11;  
  152.             //   
  153.             // txtHostKey  
  154.             //   
  155.             this.txtHostKey.Location = new System.Drawing.Point(156, 116);  
  156.             this.txtHostKey.Name = "txtHostKey";  
  157.             this.txtHostKey.Size = new System.Drawing.Size(304, 25);  
  158.             this.txtHostKey.TabIndex = 12;  
  159.             //   
  160.             // txtSourcePath  
  161.             //   
  162.             this.txtSourcePath.Location = new System.Drawing.Point(156, 151);  
  163.             this.txtSourcePath.Name = "txtSourcePath";  
  164.             this.txtSourcePath.Size = new System.Drawing.Size(304, 25);  
  165.             this.txtSourcePath.TabIndex = 13;  
  166.             //   
  167.             // txtDestinationPath  
  168.             //   
  169.             this.txtDestinationPath.Location = new System.Drawing.Point(156, 188);  
  170.             this.txtDestinationPath.Name = "txtDestinationPath";  
  171.             this.txtDestinationPath.Size = new System.Drawing.Size(304, 25);  
  172.             this.txtDestinationPath.TabIndex = 14;  
  173.             //   
  174.             // FileUpload  
  175.             //   
  176.             this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);  
  177.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  178.             this.ClientSize = new System.Drawing.Size(468, 335);  
  179.             this.Controls.Add(this.txtDestinationPath);  
  180.             this.Controls.Add(this.txtSourcePath);  
  181.             this.Controls.Add(this.txtHostKey);  
  182.             this.Controls.Add(this.txtPassword);  
  183.             this.Controls.Add(this.txtUserName);  
  184.             this.Controls.Add(this.txtHostName);  
  185.             this.Controls.Add(this.btnClose);  
  186.             this.Controls.Add(this.btnUpload);  
  187.             this.Controls.Add(this.lblStatus);  
  188.             this.Controls.Add(this.label6);  
  189.             this.Controls.Add(this.label5);  
  190.             this.Controls.Add(this.label4);  
  191.             this.Controls.Add(this.label3);  
  192.             this.Controls.Add(this.label2);  
  193.             this.Controls.Add(this.label1);  
  194.             this.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));  
  195.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;  
  196.             this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);  
  197.             this.MaximizeBox = false;  
  198.             this.MinimizeBox = false;  
  199.             this.Name = "FileUpload";  
  200.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;  
  201.             this.Text = "File Upload";  
  202.             this.ResumeLayout(false);  
  203.             this.PerformLayout();  
  204.   
  205.         }  
  206.  
  207.         #endregion  
  208.   
  209.         private System.Windows.Forms.Label label1;  
  210.         private System.Windows.Forms.Label label2;  
  211.         private System.Windows.Forms.Label label3;  
  212.         private System.Windows.Forms.Label label4;  
  213.         private System.Windows.Forms.Label label5;  
  214.         private System.Windows.Forms.Label label6;  
  215.         private System.Windows.Forms.Label lblStatus;  
  216.         private System.Windows.Forms.Button btnUpload;  
  217.         private System.Windows.Forms.Button btnClose;  
  218.         private System.Windows.Forms.TextBox txtHostName;  
  219.         private System.Windows.Forms.TextBox txtUserName;  
  220.         private System.Windows.Forms.TextBox txtPassword;  
  221.         private System.Windows.Forms.TextBox txtHostKey;  
  222.         private System.Windows.Forms.TextBox txtSourcePath;  
  223.         private System.Windows.Forms.TextBox txtDestinationPath;  
  224.     }  
  225. }  
Code for FileUpload.cs
  1. using System;  
  2. using System.Windows.Forms;  
  3. using WinSCP;  
  4.   
  5. namespace FileTransfer  
  6. {  
  7.     public partial class FileUpload : Form  
  8.     {  
  9.         public FileUpload()  
  10.         {  
  11.             InitializeComponent();  
  12.         }  
  13.   
  14.         private void BtnUpload_Click(object sender, EventArgs e)  
  15.         {  
  16.             if (string.IsNullOrEmpty(txtHostName.Text))  
  17.             {  
  18.                 lblStatus.Text = string.Format("Status : {0}""Enter Host Name !");  
  19.                 txtHostName.Select();  
  20.             }  
  21.             else if (string.IsNullOrEmpty(txtUserName.Text))  
  22.             {  
  23.                 lblStatus.Text = string.Format("Status : {0}""Enter User Name !");  
  24.                 txtUserName.Select();  
  25.             }  
  26.             else if (string.IsNullOrEmpty(txtPassword.Text))  
  27.             {  
  28.                 lblStatus.Text = string.Format("Status : {0}""Enter Password !");  
  29.                 txtPassword.Select();  
  30.             }  
  31.             else if (string.IsNullOrEmpty(txtHostKey.Text))  
  32.             {  
  33.                 lblStatus.Text = string.Format("Status : {0}""Enter Host Key !");  
  34.                 txtHostKey.Select();  
  35.             }  
  36.             else if (string.IsNullOrEmpty(txtSourcePath.Text))  
  37.             {  
  38.                 lblStatus.Text = string.Format("Status : {0}""Enter Source Path !");  
  39.                 txtSourcePath.Select();  
  40.             }  
  41.             else if (string.IsNullOrEmpty(txtDestinationPath.Text))  
  42.             {  
  43.                 lblStatus.Text = string.Format("Status : {0}""Enter Destination Path !");  
  44.                 txtDestinationPath.Select();  
  45.             }  
  46.             else  
  47.             {  
  48.                 lblStatus.Text = "Status : ";  
  49.                 try  
  50.                 {  
  51.                     // Setup session options  
  52.                     SessionOptions sessionOptions = new SessionOptions  
  53.                     {  
  54.                         Protocol = Protocol.Sftp,  
  55.                         HostName = txtHostName.Text,  
  56.                         UserName = txtUserName.Text,  
  57.                         Password = txtPassword.Text,  
  58.                         SshHostKeyFingerprint = txtHostKey.Text  
  59.                     };  
  60.   
  61.                     using (Session session = new Session())  
  62.                     {  
  63.                         // Connect  
  64.                         session.Open(sessionOptions);  
  65.   
  66.                         // Upload files  
  67.                         TransferOptions transferOptions = new TransferOptions  
  68.                         {  
  69.                             TransferMode = TransferMode.Binary  
  70.                         };  
  71.   
  72.                         TransferOperationResult transferResult;  
  73.                         transferResult =  
  74.                             session.PutFiles(txtSourcePath.Text, txtDestinationPath.Text, false, transferOptions);  
  75.   
  76.                         // Throw on any error  
  77.                         transferResult.Check();  
  78.   
  79.                         // Print results  
  80.                         foreach (TransferEventArgs transfer in transferResult.Transfers)  
  81.                         {  
  82.                             lblStatus.Text = string.Format("Upload of {0} succeeded", transfer.FileName);  
  83.                         }  
  84.                     }  
  85.                 }  
  86.                 catch (Exception ex)  
  87.                 {  
  88.                     lblStatus.Text = string.Format("Error: {0}", ex);  
  89.                 }  
  90.             }  
  91.         }  
  92.   
  93.         private void BtnClose_Click(object sender, EventArgs e)  
  94.         {  
  95.             this.Close();  
  96.         }  
  97.     }  
  98. }  
The “FileUpload” form looks like the below image.
 
File Transfer Between Windows And Linux Using WinSCP In C# 
FileDownload form.
 
Step 4
 
By using "FileDownload" form we can download files from Linux to Windows.
 
Code for FileDownload.Designer.cs
  1. namespace FileTransfer  
  2. {  
  3.     partial class FileDownload  
  4.     {  
  5.         /// <summary>  
  6.         /// Required designer variable.  
  7.         /// </summary>  
  8.         private System.ComponentModel.IContainer components = null;  
  9.   
  10.         /// <summary>  
  11.         /// Clean up any resources being used.  
  12.         /// </summary>  
  13.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>  
  14.         protected override void Dispose(bool disposing)  
  15.         {  
  16.             if (disposing && (components != null))  
  17.             {  
  18.                 components.Dispose();  
  19.             }  
  20.             base.Dispose(disposing);  
  21.         }  
  22.  
  23.         #region Windows Form Designer generated code  
  24.   
  25.         /// <summary>  
  26.         /// Required method for Designer support - do not modify  
  27.         /// the contents of this method with the code editor.  
  28.         /// </summary>  
  29.         private void InitializeComponent()  
  30.         {  
  31.             this.txtDestinationPath = new System.Windows.Forms.TextBox();  
  32.             this.txtSourcePath = new System.Windows.Forms.TextBox();  
  33.             this.txtHostKey = new System.Windows.Forms.TextBox();  
  34.             this.txtPassword = new System.Windows.Forms.TextBox();  
  35.             this.txtUserName = new System.Windows.Forms.TextBox();  
  36.             this.txtHostName = new System.Windows.Forms.TextBox();  
  37.             this.btnClose = new System.Windows.Forms.Button();  
  38.             this.btnDownload = new System.Windows.Forms.Button();  
  39.             this.lblStatus = new System.Windows.Forms.Label();  
  40.             this.label6 = new System.Windows.Forms.Label();  
  41.             this.label5 = new System.Windows.Forms.Label();  
  42.             this.label4 = new System.Windows.Forms.Label();  
  43.             this.label3 = new System.Windows.Forms.Label();  
  44.             this.label2 = new System.Windows.Forms.Label();  
  45.             this.label1 = new System.Windows.Forms.Label();  
  46.             this.SuspendLayout();  
  47.             //   
  48.             // txtDestinationPath  
  49.             //   
  50.             this.txtDestinationPath.Location = new System.Drawing.Point(148, 184);  
  51.             this.txtDestinationPath.Name = "txtDestinationPath";  
  52.             this.txtDestinationPath.Size = new System.Drawing.Size(304, 25);  
  53.             this.txtDestinationPath.TabIndex = 29;  
  54.             //   
  55.             // txtSourcePath  
  56.             //   
  57.             this.txtSourcePath.Location = new System.Drawing.Point(148, 147);  
  58.             this.txtSourcePath.Name = "txtSourcePath";  
  59.             this.txtSourcePath.Size = new System.Drawing.Size(304, 25);  
  60.             this.txtSourcePath.TabIndex = 28;  
  61.             //   
  62.             // txtHostKey  
  63.             //   
  64.             this.txtHostKey.Location = new System.Drawing.Point(148, 112);  
  65.             this.txtHostKey.Name = "txtHostKey";  
  66.             this.txtHostKey.Size = new System.Drawing.Size(304, 25);  
  67.             this.txtHostKey.TabIndex = 27;  
  68.             //   
  69.             // txtPassword  
  70.             //   
  71.             this.txtPassword.Location = new System.Drawing.Point(148, 81);  
  72.             this.txtPassword.Name = "txtPassword";  
  73.             this.txtPassword.PasswordChar = '*';  
  74.             this.txtPassword.Size = new System.Drawing.Size(304, 25);  
  75.             this.txtPassword.TabIndex = 26;  
  76.             //   
  77.             // txtUserName  
  78.             //   
  79.             this.txtUserName.Location = new System.Drawing.Point(148, 49);  
  80.             this.txtUserName.Name = "txtUserName";  
  81.             this.txtUserName.Size = new System.Drawing.Size(304, 25);  
  82.             this.txtUserName.TabIndex = 25;  
  83.             //   
  84.             // txtHostName  
  85.             //   
  86.             this.txtHostName.Location = new System.Drawing.Point(148, 16);  
  87.             this.txtHostName.Name = "txtHostName";  
  88.             this.txtHostName.Size = new System.Drawing.Size(304, 25);  
  89.             this.txtHostName.TabIndex = 24;  
  90.             //   
  91.             // btnClose  
  92.             //   
  93.             this.btnClose.Location = new System.Drawing.Point(373, 288);  
  94.             this.btnClose.Name = "btnClose";  
  95.             this.btnClose.Size = new System.Drawing.Size(75, 31);  
  96.             this.btnClose.TabIndex = 23;  
  97.             this.btnClose.Text = "Close";  
  98.             this.btnClose.UseVisualStyleBackColor = true;  
  99.             this.btnClose.Click += new System.EventHandler(this.BtnClose_Click);  
  100.             //   
  101.             // btnDownload  
  102.             //   
  103.             this.btnDownload.Location = new System.Drawing.Point(292, 288);  
  104.             this.btnDownload.Name = "btnDownload";  
  105.             this.btnDownload.Size = new System.Drawing.Size(75, 31);  
  106.             this.btnDownload.TabIndex = 22;  
  107.             this.btnDownload.Text = "Download";  
  108.             this.btnDownload.UseVisualStyleBackColor = true;  
  109.             this.btnDownload.Click += new System.EventHandler(this.BtnDownload_Click);  
  110.             //   
  111.             // lblStatus  
  112.             //   
  113.             this.lblStatus.Location = new System.Drawing.Point(17, 227);  
  114.             this.lblStatus.Name = "lblStatus";  
  115.             this.lblStatus.Size = new System.Drawing.Size(435, 25);  
  116.             this.lblStatus.TabIndex = 21;  
  117.             this.lblStatus.Text = "Status :";  
  118.             this.lblStatus.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;  
  119.             //   
  120.             // label6  
  121.             //   
  122.             this.label6.AutoSize = true;  
  123.             this.label6.Location = new System.Drawing.Point(17, 187);  
  124.             this.label6.Name = "label6";  
  125.             this.label6.Size = new System.Drawing.Size(102, 17);  
  126.             this.label6.TabIndex = 20;  
  127.             this.label6.Text = "Destination Path";  
  128.             //   
  129.             // label5  
  130.             //   
  131.             this.label5.AutoSize = true;  
  132.             this.label5.Location = new System.Drawing.Point(17, 150);  
  133.             this.label5.Name = "label5";  
  134.             this.label5.Size = new System.Drawing.Size(77, 17);  
  135.             this.label5.TabIndex = 19;  
  136.             this.label5.Text = "Source Path";  
  137.             //   
  138.             // label4  
  139.             //   
  140.             this.label4.AutoSize = true;  
  141.             this.label4.Location = new System.Drawing.Point(17, 115);  
  142.             this.label4.Name = "label4";  
  143.             this.label4.Size = new System.Drawing.Size(127, 17);  
  144.             this.label4.TabIndex = 18;  
  145.             this.label4.Text = "Host Key Fingerprint";  
  146.             //   
  147.             // label3  
  148.             //   
  149.             this.label3.AutoSize = true;  
  150.             this.label3.Location = new System.Drawing.Point(17, 84);  
  151.             this.label3.Name = "label3";  
  152.             this.label3.Size = new System.Drawing.Size(64, 17);  
  153.             this.label3.TabIndex = 17;  
  154.             this.label3.Text = "Password";  
  155.             //   
  156.             // label2  
  157.             //   
  158.             this.label2.AutoSize = true;  
  159.             this.label2.Location = new System.Drawing.Point(17, 52);  
  160.             this.label2.Name = "label2";  
  161.             this.label2.Size = new System.Drawing.Size(74, 17);  
  162.             this.label2.TabIndex = 16;  
  163.             this.label2.Text = "User Name";  
  164.             //   
  165.             // label1  
  166.             //   
  167.             this.label1.AutoSize = true;  
  168.             this.label1.Location = new System.Drawing.Point(17, 19);  
  169.             this.label1.Name = "label1";  
  170.             this.label1.Size = new System.Drawing.Size(74, 17);  
  171.             this.label1.TabIndex = 15;  
  172.             this.label1.Text = "Host Name";  
  173.             //   
  174.             // FileDownload  
  175.             //   
  176.             this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);  
  177.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  178.             this.ClientSize = new System.Drawing.Size(468, 335);  
  179.             this.Controls.Add(this.txtDestinationPath);  
  180.             this.Controls.Add(this.txtSourcePath);  
  181.             this.Controls.Add(this.txtHostKey);  
  182.             this.Controls.Add(this.txtPassword);  
  183.             this.Controls.Add(this.txtUserName);  
  184.             this.Controls.Add(this.txtHostName);  
  185.             this.Controls.Add(this.btnClose);  
  186.             this.Controls.Add(this.btnDownload);  
  187.             this.Controls.Add(this.lblStatus);  
  188.             this.Controls.Add(this.label6);  
  189.             this.Controls.Add(this.label5);  
  190.             this.Controls.Add(this.label4);  
  191.             this.Controls.Add(this.label3);  
  192.             this.Controls.Add(this.label2);  
  193.             this.Controls.Add(this.label1);  
  194.             this.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));  
  195.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;  
  196.             this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);  
  197.             this.MaximizeBox = false;  
  198.             this.MinimizeBox = false;  
  199.             this.Name = "FileDownload";  
  200.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;  
  201.             this.Text = "File Download";  
  202.             this.ResumeLayout(false);  
  203.             this.PerformLayout();  
  204.   
  205.         }  
  206.  
  207.         #endregion  
  208.   
  209.         private System.Windows.Forms.TextBox txtDestinationPath;  
  210.         private System.Windows.Forms.TextBox txtSourcePath;  
  211.         private System.Windows.Forms.TextBox txtHostKey;  
  212.         private System.Windows.Forms.TextBox txtPassword;  
  213.         private System.Windows.Forms.TextBox txtUserName;  
  214.         private System.Windows.Forms.TextBox txtHostName;  
  215.         private System.Windows.Forms.Button btnClose;  
  216.         private System.Windows.Forms.Button btnDownload;  
  217.         private System.Windows.Forms.Label lblStatus;  
  218.         private System.Windows.Forms.Label label6;  
  219.         private System.Windows.Forms.Label label5;  
  220.         private System.Windows.Forms.Label label4;  
  221.         private System.Windows.Forms.Label label3;  
  222.         private System.Windows.Forms.Label label2;  
  223.         private System.Windows.Forms.Label label1;  
  224.     }  
  225. }  
Code for FileDownload.cs
  1. using System;  
  2. using System.Windows.Forms;  
  3. using WinSCP;  
  4.   
  5. namespace FileTransfer  
  6. {  
  7.     public partial class FileDownload : Form  
  8.     {  
  9.         public FileDownload()  
  10.         {  
  11.             InitializeComponent();  
  12.         }  
  13.   
  14.         private void BtnDownload_Click(object sender, EventArgs e)  
  15.         {  
  16.             if (string.IsNullOrEmpty(txtHostName.Text))  
  17.             {  
  18.                 lblStatus.Text = string.Format("Status : {0}""Enter Host Name !");  
  19.                 txtHostName.Select();  
  20.             }  
  21.             else if (string.IsNullOrEmpty(txtUserName.Text))  
  22.             {  
  23.                 lblStatus.Text = string.Format("Status : {0}""Enter User Name !");  
  24.                 txtUserName.Select();  
  25.             }  
  26.             else if (string.IsNullOrEmpty(txtPassword.Text))  
  27.             {  
  28.                 lblStatus.Text = string.Format("Status : {0}""Enter Password !");  
  29.                 txtPassword.Select();  
  30.             }  
  31.             else if (string.IsNullOrEmpty(txtHostKey.Text))  
  32.             {  
  33.                 lblStatus.Text = string.Format("Status : {0}""Enter Host Key !");  
  34.                 txtHostKey.Select();  
  35.             }  
  36.             else if (string.IsNullOrEmpty(txtSourcePath.Text))  
  37.             {  
  38.                 lblStatus.Text = string.Format("Status : {0}""Enter Source Path !");  
  39.                 txtSourcePath.Select();  
  40.             }  
  41.             else if (string.IsNullOrEmpty(txtDestinationPath.Text))  
  42.             {  
  43.                 lblStatus.Text = string.Format("Status : {0}""Enter Destination Path !");  
  44.                 txtDestinationPath.Select();  
  45.             }  
  46.             else  
  47.             {  
  48.                 lblStatus.Text = "Status : ";  
  49.                 try  
  50.                 {  
  51.                     // Setup session options  
  52.                     SessionOptions sessionOptions = new SessionOptions  
  53.                     {  
  54.                         Protocol = Protocol.Sftp,  
  55.                         HostName = txtHostName.Text,  
  56.                         UserName = txtUserName.Text,  
  57.                         Password = txtPassword.Text,  
  58.                         SshHostKeyFingerprint = txtHostKey.Text  
  59.                     };  
  60.   
  61.                     using (Session session = new Session())  
  62.                     {  
  63.                         // Connect  
  64.                         session.Open(sessionOptions);  
  65.   
  66.                         // Upload files  
  67.                         TransferOptions transferOptions = new TransferOptions  
  68.                         {  
  69.                             TransferMode = TransferMode.Binary  
  70.                         };  
  71.   
  72.                         TransferOperationResult transferResult;  
  73.                         transferResult =  
  74.                             session.GetFiles(txtSourcePath.Text, txtDestinationPath.Text, false, transferOptions);  
  75.   
  76.                         // Throw on any error  
  77.                         transferResult.Check();  
  78.   
  79.                         // Print results  
  80.                         foreach (TransferEventArgs transfer in transferResult.Transfers)  
  81.                         {  
  82.                             lblStatus.Text = string.Format("Upload of {0} succeeded", transfer.FileName);  
  83.                         }  
  84.                     }  
  85.                 }  
  86.                 catch (Exception ex)  
  87.                 {  
  88.                     lblStatus.Text = string.Format("Error: {0}", ex);  
  89.                 }  
  90.             }  
  91.         }  
  92.   
  93.         private void BtnClose_Click(object sender, EventArgs e)  
  94.         {  
  95.             this.Close();  
  96.         }  
  97.     }  
  98. }  
The “FileUpload” form looks like the below image.
 
File Transfer Between Windows And Linux Using WinSCP In C#


Similar Articles