繁体   English   中英

在 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();
}

从以下问题的代码开始,并在文件名前缀上添加附加约束。
在 C# 中使用 SSH.NET SFTP 下载目录

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 库。 例如,使用我的WinSCP .NET 程序集,您可以对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");
                    }               

                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM