Check is file transfer with SSH.NET was successful or not

Viewed 171

I am working on SSH.NET file Upload function in .NET CORE 6 application. It is all working fine. To find status as if the file is loaded successfully, I am calling event from Upload method however I am always getting "24" and not sure what that mean and also I have implemented correctly

public bool UploadFile(Tenant destinationTenant, string processedFilePath)
{
    string fileStream = string.Empty;
    bool isFileUploaded = false;
    var remoteFilePath = destinationTenant.RemoteDirectoryPath;

    var sftpClient = sftpClients.FirstOrDefault(_ => _.Key == destinationTenant.TenantId.ToString()).Value;

    try
    {
        if(sftpClient != null)
        {
            sftpClient.Connect();

            if (sftpClient.IsConnected)
            {
                sftpClient.ChangeDirectory(remoteFilePath);

                using(FileStream fs = new FileStream(processedFilePath, FileMode.Open))
                {
                    sftpClient.BufferSize = 4 * 2024;
                    sftpClient.UploadFile(fs, Path.GetFileName(processedFilePath), (o) =>
                    {
                        var ff = o.ToString(); // need help here!
                        var fff = o;
                        Console.WriteLine(o.ToString());
                    });
                }

                Console.WriteLine($"Finished Uploading UBW File [FileName] At Location [{destinationTenant.RemoteDirectoryPath}]");
            }
            else
            {
                Console.Write($"Unable To Connect SFTP Server [{destinationTenant.FileServer.Name}]; Dated [{DateTime.UtcNow}]");
            }
        }
        else
        {
            Console.Write($"Unable To Retrieve SFTP Client Configuration; Dated [{DateTime.UtcNow}] ");
        }
    }
    catch(Exception ex)
    {
        throw; 
    }
    finally
    {
        if (sftpClient != null)
        { sftpClient.Disconnect(); }
    }

    return isFileUploaded;
}
1 Answers

The SftpClient.UploadFile throws an exception, if anything goes wrong.

That's how you tell, if the upload succeeded or not.

Not via the uploadCallback.

Related