在 C# 中使用 SSH.NET 下载以前缀名称开头的 SFTP 文件

Download SFTP files that starts with prefix name using SSH.NET in C#

我正在使用 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();
}

从以下问题的代码开始,并在文件名前缀上添加额外的约束。

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, you can do the same with a single call to Session.GetFilesToDirectory:

session.GetFilesToDirectory(remotePath, localPath, prefix + "*").Check();
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");
                    }               

                }