简体   繁体   中英

Convert string or list to Renci.SshNet.Sftp

I want to download the specific files from SFTP server(root and Subdirectories). I am trying to get a list of files and subdirectories from SFTP recursively which i could not do. but with the following code it is only listing only files and directory names. I am trying to add the directory path to a directory of type (Renci.SshNet.Sftp.SftpFile) I am getting the following exception.

Any alternative way to handle this issue.

using (var sftp = new SftpClient(host, 22, username, password))
   {
      sftp.Connect();
      var directory = sftp.ListDirectory("/").ToList();
      string FileName = "Dir1/abc.txt,file1.txt,file2.txt"
      var fileNameList = FileName.Contains(',') ? FileName.Split(',') : String.IsNullOrEmpty(FileName) ? new string[0] : new string[1] { FileName };
      
      HashSet<string> filenameHash = new HashSet<string>(fileNameList.Select(x => x));
      directory.RemoveAll(x => !filenameHash.Contains(x.Name));
      directory.RemoveAll(x => (!x.IsRegularFile));
      
      string dir = "";
      
      SftpFile fl = (SftpFile)Convert.ChangeType(dir, typeof(SftpFile));
      
      foreach (var f  in fileNameList)
                {
                    if (f.Contains('/'))
                    {
                        fl += f;
                                
                    }
                }

            

Exception:

System.InvalidCastException: 'Invalid cast from 'System.String' to 'Renci.SshNet.Sftp.SftpFile'.'

I fixed the issue just by concatenating(subdirectory to directory) and reassigning it to the directory.

using (var sftp = new SftpClient(host, 22, username, password))
   {
      sftp.Connect();
      var directory = sftp.ListDirectory("/").ToList();
      string FileName = "Dir1/abc.txt,file1.txt,file2.txt"
      var fileNameList = FileName.Contains(',') ? FileName.Split(',') : String.IsNullOrEmpty(FileName) ? new string[0] : new string[1] { FileName };
      
      HashSet<string> filenameHash = new HashSet<string>(fileNameList.Select(x => x));
      directory.RemoveAll(x => !filenameHash.Contains(x.Name));
      directory.RemoveAll(x => (!x.IsRegularFile));
      
      string dir = "";
      
      SftpFile fl = (SftpFile)Convert.ChangeType(dir, typeof(SftpFile));
      
      foreach (var f  in fileNameList)
                {
                    if (f.Contains('/'))
                    {
                        string[] fl = f.Split("/");
                        var subdirectory = sftp.ListDirectory(f.Split('/')[0]).ToList();
                        directory = directory.Concat(subdirectory).ToList();
                    }
                }
 private void RecursivelyFilechecking(string Filepath, string _todaydate, string hostid, bool isHeader, string hostName, Int32 CustomerId)
        {
            try
            {
                
                if (_sftp.Exists(Filepath))
                {
                    var files = _sftp.ListDirectory(Filepath);
                    
                    bool flg = false;
                    string JobMailBody;
                    string toadd = string.Empty;
                    string fileName = string.Empty;
                    string fileDate = string.Empty;
                    Int32 filepathId = 0;
                   
                    foreach (var file in files)
                    {
                        if (file.IsDirectory && !file.Name.Equals(".") && !file.Name.Equals(".."))
                        {
                           
                            RecursivelyFilechecking(file.FullName, _todaydate, hostid, isHeader, hostName, CustomerId);
                        }
                        else if (!file.Name.Equals(".") && !file.Name.Equals(".."))
                        {
                          
                            if (file.LastWriteTime.ToString("ddMMyyyy").Equals(_todaydate))
                            {

                    
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                
            }
        }

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