Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 416
Joe Allen Avatar asked Apr 02 '10 02:04

Joe Allen


People also ask

How do I delete a file in SFTP?

delete( s , filename ) deletes the specified file from the current folder on the SFTP or FTP server associated with s .

How do I delete files from remote SFTP server?

To delete a file on the server, type "rm" and then the filename. Syntax: psftp> rm filename.

How do I delete multiple files from SFTP server?

You can delete multiple files also using del. file1.sh, file2.sh, file3.sh, file4.sh, file5.sh will be deleted in /home/user1.


1 Answers

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);
like image 88
jbehren Avatar answered Oct 22 '22 23:10

jbehren