How to delete a file from a SFTP server programmatically using SharpSSH?

Joe Allen picture Joe Allen · Apr 2, 2010 · Viewed 12.4k times · Source

How to delete a file from a SFTP server using Tamir Gal's SharpSSH? I have been able to accomplish other functionality but deletion.

Answer

jbehren picture jbehren · Jun 28, 2011

The SshExec class didn't work for me, but a little Reflection magic worked:

var prop = sftp.GetType().GetProperty("SftpChannel", BindingFlags.NonPublic | BindingFlags.Instance);
var methodInfo = prop.GetGetMethod(true);
var sftpChannel = methodInfo.Invoke(sftp, null);
((ChannelSftp) sftpChannel).rm(ftpPath);