Skip to main content

Curiosus.SFTP

NuGet Downloads Coverage

Abstractions for working with files over SFTP: ISftpClient, ISftpClientFactory, SftpClientOptions and SftpFileInfo. Reference it from code that reads or writes SFTP files; the implementation lives in Curiosus.SFTP.SSH.NET.

Installation​

dotnet add package Curiosus.SFTP

Usage​

public class ReportUploader
{
private readonly ISftpClientFactory _sftpClientFactory;

public ReportUploader(ISftpClientFactory sftpClientFactory)
{
_sftpClientFactory = sftpClientFactory;
}

public async Task UploadAsync(byte[] report)
{
using var client = _sftpClientFactory.GetSftpClient();

await client.UploadFileToServerAsync(report, "/reports", "2026/report.csv", overwrite: true);

foreach (var file in client.ListDirectoryContents("/reports/2026"))
{
if (file.Type == SftpFileType.Regular)
{
Console.WriteLine($"{file.FullName}: {file.Length} bytes, {file.LastWriteTimeUtc:u}");
}
}
}
}

ISftpClient operations take a base directory and a file name relative to it: UploadFileToServerAsync (bytes or stream), DownloadFileFromServer / DownloadFileFromServerAsync (returns null if the file does not exist), GetDownloadStream, DeleteFileFromServer, IsExist, ListDirectoryContents and CheckConnection. Clients are IDisposable.

SftpClientOptions:

OptionDescription
SshServer, SshPort, SshLoginServer, port and user name (server and login are required).
SshPasswordPassword authentication.
SshPrivateKeyPath, SshPrivateKeyPassphrasePrivate key authentication (used when no password is set).
RetryCount, RetryTimeoutSecRetries on socket and SSH errors (default 3 retries, 5 seconds apart).
CheckOnStartCheck the connection on application start (default true).

Either SshPassword or SshPrivateKeyPath must be specified.

See also​