Question
Are there any C# open-source components that allow me to delete files via SFTP?
Solution
Try SharpSSH.
OTHER TIPS
Tamir Gal's Sharp SSH is quite popular open source implementation of SFTP for .NET. Give it a try.
If you preffer fully supported commercial component can try our Rebex SFTP. Following code ilustrates the concept:
using Rebex.Net;
// create client and connect
Sftp client = new Sftp();
client.Connect(hostname);
client.Login(username, password);
// delete the file
client.DeleteFile("/path/to/the/file");
// disconnect
client.Disconnect();
You can use OpenSSH and issue sftp batch commands. All you have to do on the c# side is launch the sftp process with the correct command line.
I have been using http://sshnet.codeplex.com/. It has worked well for me and is actively being developed / supported.
The code to delete the file is as simple as
public static void DownloadFile(SftpClient client, SftpFile remoteFileName)
{
var localFileName = System.IO.Path.GetFileName(remoteFileName.Name );
using (var file = File.OpenWrite(localFileName))
{
client.DownloadFile(remoteFileName.FullName , file);
remoteFileName.Delete();
}
}
Execute Linux command rm with object SshExec. This command delete file. Example:
rm /dir1/dir2/file.txt
Other Example Tamir Execute Command
public static bool DeleteFile(string remotePath)
{
try
{
SshExec comando = new SshExec(Server, User);
comando.Password = Password;
comando.Connect();
string paso = comando.RunCommand("rm " + remotePath);
comando.Close();
return true;
}
catch (Exception ex)
{
mErrorSFTP = ex.Message;
return false;
}
}