C#WinSCP .NET程序集从最新目录下载最新文件

时间:2016-02-28 17:52:17

标签: c# ftp winscp winscp-net

我是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" 

现在我设定了固定的路径,为解决方案而努力。

1 个答案:

答案 0 :(得分:2)

扩展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