Get list of files from SFTP using Winscp(Version 5.5.4) in C#

Category: C#.Net Comments: No comments

Use below link to download DLL and EXE
Download Link :- http://olex.openlogic.com/packages/winscp/5.5.4
Dll :- WinSCPnet.dll :-> Select Winscp 5.5.4 .NET Assembly / COM library Windows ALL Binary
Executable Path :- WinSCP.exe :-> Select Winscp 5.5.4 Portable Executable Windows ALL Binary

Use WinSCP;//Namespace after adding Dll in project

Below function used to create the SFTP Connectivity :-
public static Session SFTPConnectivity()
{
try
{
// Setup session options
TimeSpan ts = new TimeSpan(1,1,1);//Set as per requirement
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = “sftp4.abdul.com”,
UserName = “Abdul”,
Password = “Abdul”,
GiveUpSecurityAndAcceptAnySshHostKey = true,
PortNumber = 22,
Timeout = ts
};
Session session = new Session()
{
Timeout = ts,
ExecutablePath = “D:\Abdul\Winscp\Winscp.exe”,
};
session.Open(sessionOptions);
return session;
}
catch (Exception ex)
{

throw ex;
}
}

Below function used to get the list of files from SFTP with the help of created sftp session
public static List<string> ListOfFilesFromDirctory(Session session)
{
try
{
if (session != null)
{
string fileDirectoryPath = “/NET/Abdul/Files/”;
RemoteDirectoryInfo SapDirectory = session.ListDirectory(fileDirectoryPath);
List<string> listFileName = new List<string>();
foreach (RemoteFileInfo fileInfo in SapDirectory.Files)
{
listFileName.Add(fileInfo.Name);
}
}
return listFileName;
}
catch (Exception ex)
{
throw ex;
}

}

Tags:  

Leave a Reply

Your email address will not be published. Required fields are marked *

*

HTML tags are not allowed.