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