SSH.NET SFTP Get a list of directories and files recursively

后端 未结 5 1719
不思量自难忘°
不思量自难忘° 2021-02-13 13:50

I am using Renci.SshNet library to get a list of files and directories recursively by using SFTP. I can able to connect SFTP site but I am not sure how to get a list of director

5条回答
  •  [愿得一人]
    2021-02-13 14:39

    I have achieved this using recursion. Created a class TransportResponse like this

     public class TransportResponse
    {
        public string directoryName { get; set; }
        public string fileName { get; set; }
        public DateTime fileTimeStamp { get; set; }
        public MemoryStream fileStream { get; set; }
        public List lstTransportResponse { get; set; }
    }
    

    I create a list of TransportResponse class. If the directoryName is not null, it will contain a list of the same class which will have the the files inside that directory as a MemoryStream ( this can be changed as per your use case)

    List lstResponse = new List();
    using (var client = new SftpClient(connectionInfo))
      {
              try
              {
                        Console.WriteLine("Connecting to " + connectionInfo.Host + " ...");
                        client.Connect();
                        Console.WriteLine("Connected to " + connectionInfo.Host + " ...");
               }
               catch (Exception ex)
               {
                        Console.WriteLine("Could not connect to "+ connectionInfo.Host +" server. Exception Details: " + ex.Message);
               }
               if (client.IsConnected)
               {
                        var files = client.ListDirectory(transport.SourceFolder);
                        lstResponse = downloadFilesInDirectory(files, client);
                        client.Disconnect();
                }
                else
                {
                        Console.WriteLine("Could not download files from "+ transport.TransportIdentifier +" because client was not connected.");
                 }
       }
    
    
    
    private static List downloadFilesInDirectory(IEnumerable files, SftpClient client)
        {
            List lstResponse = new List();
            foreach (var file in files)
            {
                if (!file.IsDirectory)
                {
                    if (file.Name != "." && file.Name != "..")
                    {
                        if (!TransportDAL.checkFileExists(file.Name, file.LastWriteTime))
                        {
                            using (MemoryStream fs = new MemoryStream())
                            {
                                try
                                {
                                    Console.WriteLine("Reading " + file.Name + "...");
                                    client.DownloadFile(file.FullName, fs);
                                    fs.Seek(0, SeekOrigin.Begin);
                                    lstResponse.Add(new TransportResponse { fileName = file.Name, fileTimeStamp = file.LastWriteTime, fileStream = new MemoryStream(fs.GetBuffer()) });
                                }
                                catch(Exception ex)
                                {
                                    Console.WriteLine("Error reading File. Exception Details: " + ex.Message);
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("File was downloaded previously");
                        }
                    }
                }
                else
                {
                    if (file.Name != "." && file.Name != "..")
                    {
                        lstResponse.Add(new TransportResponse { directoryName = file.Name,lstTransportResponse = downloadFilesInDirectory(client.ListDirectory(file.Name), client) });
                    }                
                }
            }
    
            return lstResponse;
        }
    

    Hope this helps. Thanks

提交回复
热议问题