简体   繁体   中英

Unable to delete files from a remote directory with SFTP/SSH.NET?

I am creating a C# windows form applications with multiple textboxes, where I am saving the user input as a JSON. I am retrieving the user input from a JSON file like that, following deserialization:

string host2 = currentList[1].IPaddress;
string username2 = currentList[1].username;
string password2 = currentList[1].password;
string remoteDirectory2 = currentList[1].sourcefolder;
string localDirectory2 = currentList[1].destfolder;
string filextension2 = currentList[1].filextension;
string removedownloaded2 = currentList[1].removedownloaded.ToString();

This is my json string structure.

{
    "Record": 2,
    "IPaddress": "192.168.6.247",
    "Machinename": "taurus",
    "username": "root",
    "password": "root",
    "sourcefolder": "/home/root/bin",
    "destfolder": "D:/DataProfiler_Nautitech/Files",
    "filextension": ".sh",
    "removedownloaded": 1
  }

The target is like below:

  1. Connect to SFTP server.

  2. Download files to local server.

  3. If removedownloaded == 1, delete those files.

  4. If removedownloaded == 0, keep those files.

I have tried below method

if (removedownloaded2 == "1")
    {
        //First method
        sftp2.Delete(path);
        //Second method
        sftp2.DeleteDirectory(path);
        //Third method
        sftp2.DeleteFile(path);
    }

but none of these deleted any files.

This is the full code: (failed!)

using (SftpClient sftp2 = new SftpClient(host2, username2, password2))
            {
                try
                {
                    sftp2.Connect();
                    Console.WriteLine("Machine 2 - Connected");
                    var files = sftp2.ListDirectory(remoteDirectory2);

                    foreach (var file in files)
                    {
                        try
                        {
                            string remoteFileName = file.Name;
                            if ((file.Name.EndsWith(filextension2)))
                            {
                                using (Stream file1 = File.OpenWrite(Path.Combine(localDirectory2, remoteFileName)))
                                {

                                    string path = remoteDirectory2 + "/" + remoteFileName;
                                    sftp2.DownloadFile(path, file1);

                                    if (removedownloaded2 == "1")
                                    {
                                        //First method
                                        sftp2.Delete(path);
                                        //Second method
                                        //sftp2.DeleteDirectory(path);
                                        //Third method
                                        //sftp2.DeleteFile(path);
                                    }
                                }
                            }

                        }
                        catch (Exception er1)
                        {
                            //MessageBox.Show("An exception has been caught " + er1.ToString());
                        }

                    }
                }
                catch (Exception entry)
                {
                    MessageBox.Show(entry.Message);
                }
                //finally
                //{
                //    sftp2.Disconnect();
                //}
            }

Any thoughts on how to delete files after downloading them? Thanks in advance for any help.

Faced the same issue. Closing the stream returned from the OpenWrite command allowed the file to be deleted in my case. Having the delete code after the using block might solve this issue here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM