0 votes
by (120 points)

We are moving from biztalk to azure. In biztalk we are using nsoftware SFTP adapter which has a property called "MonitorFileGrowth = true" , it will make sure if the file is under writing , it will not pick up the file rather it will make a note of the size of the file.
It picks up in the next polling if the size is same.

Is there any way we can implement this by using Rebex Library for SFTP in C#.

Applies to: Rebex SFTP

1 Answer

0 votes
by (1.2k points)
edited by

Hello,
this is example implementation of how something like this could be done. You can set timeouts in the loop to poll periodically and not all the time.

using var client = new Sftp();

client.Connect("test.rebex.net");
client.Login("demo", "password");

var files = new List<RemoteFile>()
{
    new RemoteFile("/pub/example/readme.txt", client),
    new RemoteFile("/pub/example/KeyGenerator.png", client),
    new RemoteFile("/pub/example/ResumableTransfer.png", client)
};

TimeSpan delay = TimeSpan.FromSeconds(5); // set a safe value for you
while (files.Count > 0)
{
    Thread.Sleep(delay); // wait a while 

    files = TryDownload(files, @"c:\data", client);
}

List<RemoteFile> TryDownload(List<RemoteFile> files, string targetFolder, Sftp client)
{
    List<RemoteFile> updatedFiles = new List<RemoteFile>();

    foreach (var file in files)
    {
        var size = client.GetFileLength(file.Path);
        if (size == file.Size)
        {
            client.Download(file.Path, targetFolder);
        }
        else
        {
            file.Size = size;
            updatedFiles.Add(file);
        }
    }

    return updatedFiles;
}

class RemoteFile
{
    public string Path;
    public long Size;

    public RemoteFile(string path, Sftp client)
    {
        Path = path;
        Size = client.GetFileLength(path);
    }
}
...