Remotely delete a file through SFTP using a C# program

17,598

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();
    }
}
Share:
17,598
User2012384
Author by

User2012384

Any fool can write code that a computer can understand. Good programmers write code that humans can understand. General questions lead to general answers, specific questions lead to specific answers. If you want to receive a good answer, please learn how to ask a good question Hello all I'm an system analyst in Hong Kong, somewhere in Asia

Updated on June 05, 2022

Comments

  • User2012384
    User2012384 over 1 year

    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