Remotely delete a file through SFTP using a C# program

User2012384 picture User2012384 · Apr 16, 2012 · Viewed 16.7k times · Source

I want to ask how can I remotely delete a file using sftp I have tried using SharpSSH but it doesn't work, I got SftpException

i added this code first in the sftp.cs first

    public void Delete(string path)
    {
        SftpChannel.rm(path);
    }

then i typed this in the program

Sftp ftp = new Sftp("ip address", "username", "password"); ftp.Connect(); ftp.Delete("path");

Thanks, The problem was solved the problem was I forgot to put a "/" in front of the path, so it fails

Answer

dtown123 picture dtown123 · Apr 16, 2012

I use Renci.SshNet for my SFTP duties. It works really well for me. Here's an example of what you're trying to do:

using Renci.SshNet;
using Renci.SshNet.Sftp;

public void DeleteFile(string server, int port, string username, string password, string sftpPath)
{
    using (SftpClient sftpClient = new SftpClient(server, port, username, password))
    {
        sftpClient.Connect();
        sftpClient.DeleteFile(sftpPath);
        sftpClient.Disconnect();
    }
}