Reply to thread

I am retrieving the machine details 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")

                                    {

                                        sftp2.Delete(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.


Continue reading...


Back
Top