【问题标题】:Download SFTP files that starts with prefix name using SSH.NET in C#在 C# 中使用 SSH.NET 下载以前缀名称开头的 SFTP 文件
【发布时间】:2020-06-03 22:52:09
【问题描述】:

我正在使用 SSH.NET 库从 SFTP 服务器下载文件。当我给它完整的文件名时,它可以工作。但我想下载一个带有前缀名称的文件,在该文件夹中,前缀名称是POS_ETH_SE7*。总会有一个文件。下载后,我将其移至另一个文件夹。这是我的方法:

var auth = new PasswordAuthenticationMethod(username, password);
var connectionInfo = new ConnectionInfo(ipAddress, port, auth);

// Upload File
using (var sftp = new SftpClient(connectionInfo))
{
    string pathLocalFile =
        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
            "POS_ETH_SE7.ics");

    sftp.Connect();

    Console.WriteLine("Downloading {0}", remoteFilePath);

    using (Stream fileStream = File.OpenWrite(pathLocalFile))
    using (StreamWriter writer = new StreamWriter(fileStream))

    {
        try
        {
            sftp.DownloadFile(remoteFilePath, fileStream);
        }
        catch (SftpPathNotFoundException ex)
        {

        }
    }
    try
    {
        var inFile = sftp.Get(remoteFilePath);
        inFile.MoveTo(remoteMoveFileToPath + "/POS_ETH_SE7.xml");
    }
    catch (SftpPathNotFoundException ex)
    {
        Console.WriteLine("\nnothing to update...\n");
    }

    sftp.Disconnect();
}

【问题讨论】:

    标签: c# .net sftp ssh.net


    【解决方案1】:

    从以下问题的代码开始,在文件名前缀上添加附加约束。
    Downloading a directory using SSH.NET SFTP in C#

    const string prefix = "POS_ETH_SE7";
    IEnumerable<SftpFile> files = client.ListDirectory(remotePath);
    files = files.Where(file => file.Name.StartsWith(prefix));
    foreach (SftpFile file in files)
    {
        string pathLocalFile = Path.Combine(localPath, file.Name);
    
        using (var stream = File.Create(pathLocalFile))
        {
            client.DownloadFile(file.FullName, stream);
        }
    
        // If you want to archive the downloaded files:
        string archivePath = remoteMoveFileToPath + "/" + file.Name;
        client.RenameFile(file.FullName, archivePath);
    }
    

    或者使用更强大的 SFTP 库。例如,使用 my WinSCP .NET assembly,您可以通过一次调用 Session.GetFilesToDirectory 来执行相同的操作:

    session.GetFilesToDirectory(remotePath, localPath, prefix + "*").Check();
    

    【讨论】:

      【解决方案2】:
      using (var sftp = new SftpClient(connectionInfo))
                  {
                      sftp.Connect();
                      IEnumerable<SftpFile> files = sftp.ListDirectory(configSftpClient.remoteFilePath);
                      files = files.Where(file => file.Name.StartsWith(configSftpClient.filePrefix));
      
      
                      foreach (SftpFile file in files)
                      {
                          string pathLocalFile = Path.Combine(configSftpClient.localFilePath, file.Name);
                          try
                          {
                              using (var stream = File.Create(pathLocalFile))
                              {
                                  sftp.DownloadFile(file.FullName, stream);
                                  var movableFile = sftp.Get(file.FullName);
                                  Console.WriteLine(file.FullName);
                                  movableFile.MoveTo(configSftpClient.remoteMoveFileToPath + "/" + file.Name);
                                  stream.Close();
                              }
                          }
                          catch(Exception ex)
                          {
                              Console.WriteLine("file used by other");
                          }               
      
                      }
      

      【讨论】:

      • 因为我下载文件后想将它移动到服务器中的另一个文件夹
      • 我已经更新了我的答案以包含重命名,所以这一切都在一篇文章中。
      猜你喜欢
      • 2019-02-18
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      相关资源
      最近更新 更多