1. Open Visual studio and create a new website
2. Right Click to your solution and go to Manage Nuget Packages
3. Search for SSH.NET and install it
4. Include namespaces like using Renci.SshNet; and using System.IO;
5. On your Button Click Event write below code
string port = SFTP Port;
string host = SFTP Host;
string username = SFTP Username;
string password = SFTP Password;
string remoteDirectory = SFTP Folder from where the file will be downloaded;
string localDirectory = Local Directory Path;
if (!File.Exists(Path.GetDirectoryName(localDirectory)))
Directory.CreateDirectory(Path.GetDirectoryName(localDirectory));
using (SftpClient sftp = new SftpClient(host, Convert.ToInt32(port), username, password))
{
sftp.Connect();
var files = sftp.ListDirectory(remoteDirectory);
foreach (var file in files)
{
string remoteFileName = file.Name;
if (file.Name.Contains("."))
{
using (Stream file1 = File.OpenWrite(localDirectory + "\\" + remoteFileName))
{
sftp.DownloadFile(remoteDirectory + "\\" + remoteFileName, file1);
}
}
}
}
6. Run Your Project
2. Right Click to your solution and go to Manage Nuget Packages
3. Search for SSH.NET and install it
4. Include namespaces like using Renci.SshNet; and using System.IO;
5. On your Button Click Event write below code
string port = SFTP Port;
string host = SFTP Host;
string username = SFTP Username;
string password = SFTP Password;
string remoteDirectory = SFTP Folder from where the file will be downloaded;
string localDirectory = Local Directory Path;
if (!File.Exists(Path.GetDirectoryName(localDirectory)))
Directory.CreateDirectory(Path.GetDirectoryName(localDirectory));
using (SftpClient sftp = new SftpClient(host, Convert.ToInt32(port), username, password))
{
sftp.Connect();
var files = sftp.ListDirectory(remoteDirectory);
foreach (var file in files)
{
string remoteFileName = file.Name;
if (file.Name.Contains("."))
{
using (Stream file1 = File.OpenWrite(localDirectory + "\\" + remoteFileName))
{
sftp.DownloadFile(remoteDirectory + "\\" + remoteFileName, file1);
}
}
}
}
6. Run Your Project
No comments:
Post a Comment
Thanks