簡體   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