简体   繁体   中英

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

I'm new to WinSCP. I'm facing difficulty of making the remote path to dynamic because the folder in my FTP is generated following by root/data/20160222/00(hour)/00(minute)/test.json*

This path also always contain more than one files.

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" 

Now I set fixed path, struggle for solution.

Expanding on the WinSCP .NET assembly example for Downloading the most recent file :

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;
}

See also the documentation for Session.ListDirectory .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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