Home Forums .NET libraries Xceed SFTP/FTP for .NET Unable to RemoveDirectory if the folder is not empty

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #21513 |

    Hi,

     I am trying to remove Folder at the remote file system using the following code:

    using (SFtpSession sftpSession = new SFtpSession(_sshClient))
    {
            AbstractFolder remoteFolder = new SFtpFolder(sftpSession);
            var path = remoteFolder + SelectedPath.Substring(1);
            sftpSession.SFtpClient.RemoveDirectory(path);

     It throws an exception that, the  selected path is not empty. I am using Xceed SFtpv5.5 for the above operation.

    This works perfectly fine when the directory is empty. But does not work if the directory I am trying to delete has a folder / file inside it. 

    I also use Xceed Ftpv5.3  and I have not faced such an issue.

     Any help to fix this would be appreciated.

     

    Thanks,

    Rahul 

    Applies to Xceed SFTP for .NET. Imported from legacy forums. Posted by Rahul (had 423 views)

    Diane [Xceed]
    Moderator
    Post count: 1353

    Hi Rahul,

    On most server systems, you cannot delete a non-empty folder. The folder’s contents must be deleted first. The FileSystem API can perform those operations for you by using the Delete() method like so:

       AbstractFolder remoteFolder = new SFtpFolder( sftpSession, “path to the selected folder” );

       if( remoteFolder.Exists )
       {
          remoteFolder.Delete();
       }

    This will recursively delete all the files and subfolders in the folder and then the folder itself.

    Applies to Xceed SFTP for .NET. Imported from legacy forums. Posted by Diane [Xceed] (had 620 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.