Remove or Delete file 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 remove file from SFTP with the help of created sftp session
public static void RemoveFileFromSFTP(Session session)
{
try
{
string filePath = string.Empty;
if (session != null)
{
filePath = “/NET/Abdul/Files/Abdul.txt”;
session.RemoveFiles(filePath);
}
}
catch (Exception ex)
{
throw ex
}
}

Leave a Reply

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

*

HTML tags are not allowed.