{"id":2828,"date":"2015-03-06T17:09:05","date_gmt":"2015-03-06T17:09:05","guid":{"rendered":"http:\/\/www.csidata.com\/?page_id=2828"},"modified":"2025-07-29T00:31:10","modified_gmt":"2025-07-29T00:31:10","slug":"how-to-download-files-from-ftp-or-sftp-in-c","status":"publish","type":"page","link":"https:\/\/www.csidata.com\/?page_id=2828","title":{"rendered":"How to download files from SFTP in C#"},"content":{"rendered":"<p>The server allows only 2 concurrent sessions per user. Also there is a 2 minute timeout for idle connections. Your code must account for these otherwise the server will disconnect the session.<\/p>\n<p>First you must install the SSH.Net library from NuGet. Add the .dll as a reference to your project.<\/p>\n<div style=\"border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;\">\n<div style=\"background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px;\">Code Snippet<\/div>\n<div style=\"background: #fff; overflow: auto;\">\n<ol style=\"background: #ffffff; margin: 0; padding: 0 0 0 5px; list-style: none;\" start=\"1\">\n<li style=\"background: #f3f3f3;\"><span style=\"color: #0000ff;\">using<\/span> System.IO;<\/li>\n<li><span style=\"color: #0000ff;\">using<\/span> Renci.SshNet;<\/li>\n<li style=\"background: #f3f3f3;\"><span style=\"color: #0000ff;\">using<\/span> Renci.SshNet.Common;<\/li>\n<li><span style=\"color: #0000ff;\">using<\/span> Renci.SshNet.Sftp;<\/li>\n<li style=\"background: #f3f3f3;\"><\/li>\n<li><span style=\"color: #2b91af;\">String<\/span> Host = <span style=\"color: #a31515;\">&#8220;ftp.csidata.com&#8221;<\/span>;<\/li>\n<li style=\"background: #f3f3f3;\"><span style=\"color: #0000ff;\">int<\/span> Port = 22;<\/li>\n<li><span style=\"color: #2b91af;\">String<\/span> RemoteFileName = <span style=\"color: #a31515;\">&#8220;TheDataFile.txt&#8221;<\/span>;<\/li>\n<li style=\"background: #f3f3f3;\"><span style=\"color: #2b91af;\">String<\/span> LocalDestinationFilename = <span style=\"color: #a31515;\">&#8220;TheDataFile.txt&#8221;<\/span>;<\/li>\n<li><span style=\"color: #2b91af;\">String<\/span> Username = <span style=\"color: #a31515;\">&#8220;yourusername&#8221;<\/span>;<\/li>\n<li style=\"background: #f3f3f3;\"><span style=\"color: #2b91af;\">String<\/span> Password = <span style=\"color: #a31515;\">&#8220;yourpassword&#8221;<\/span>;<\/li>\n<li><\/li>\n<li style=\"background: #f3f3f3;\"><span style=\"color: #0000ff;\">using<\/span> (<span style=\"color: #0000ff;\">var<\/span> sftp = <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">SftpClient<\/span>(Host, Port, Username, Password))<\/li>\n<li>{<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0sftp.Connect();<\/li>\n<li><\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">using<\/span> (<span style=\"color: #0000ff;\">var<\/span> file = <span style=\"color: #2b91af;\">File<\/span>.OpenWrite(LocalDestinationFilename))<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0{<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sftp.DownloadFile(RemoteFileName, file);<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0}<\/li>\n<li style=\"background: #f3f3f3;\"><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0sftp.Disconnect();<\/li>\n<li style=\"background: #f3f3f3;\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The server allows only 2 concurrent sessions per user. Also there is a 2 minute timeout for idle connections. Your code must account for these otherwise the server will disconnect the session. First you must install the SSH.Net library from NuGet. Add the .dll as a reference to your project. Code Snippet using System.IO; using [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/www.csidata.com\/index.php?rest_route=\/wp\/v2\/pages\/2828"}],"collection":[{"href":"https:\/\/www.csidata.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csidata.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csidata.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csidata.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2828"}],"version-history":[{"count":12,"href":"https:\/\/www.csidata.com\/index.php?rest_route=\/wp\/v2\/pages\/2828\/revisions"}],"predecessor-version":[{"id":5135,"href":"https:\/\/www.csidata.com\/index.php?rest_route=\/wp\/v2\/pages\/2828\/revisions\/5135"}],"wp:attachment":[{"href":"https:\/\/www.csidata.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}