C# WinSCP .NET assembly 从最新目录下载最新文件

C# WinSCP .NET assembly Download the latest file from the latest directory

我是 WinSCP 的新手。我在将远程路径设置为动态时遇到困难,因为我的 FTP 中的文件夹是在 root/data/20160222/00(hour)/00(minute)/test.json*

之后生成的

此路径也总是包含多个文件。

SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "192.168.1.100",
    UserName = "admin",  
    Password = "admin",      
};

string localPath = @"c:\gatewayftp\json";
// this path needs to take the latest date and the latest hour and minutes every day
string remotePath = "/data/20160228/2100/59" 

现在我设置固定路径,求解决方案

扩展 Downloading the most recent file 的 WinSCP .NET 程序集示例:

string remotePath = "/data";
// In each of three levels of hierarchy...
for (int i = 0; i < 3; i++)
{
    // ... pick the last file/directory alphabetically
    // (use .LastWriteTime instead of .Name to pick the latest file/directory by time)
    remotePath +=
        "/" + 
        session.ListDirectory(remotePath).Files
            .OrderByDescending(file => file.Name).First().Name;
}

另见 documentation for Session.ListDirectory