Skip to content
Auto
TagBites.IO

SFTP file system

This file system provides an implementation for the file system based on the SFTP protocol using SSH.NET library.

Password authentication:

var address = "address";
var username = "username";
var password = "password";
using(FileSystem fileSystem = TagBites.IO.Sftp.SftpFileSystem.Create(address, username, password))
{
// ... using like standard file system
}

Private key authentication:

using Renci.SshNet;
var keyFile = new PrivateKeyFile("path/to/private-key");
using(FileSystem fileSystem = TagBites.IO.Sftp.SftpFileSystem.Create(address, username, keyFile))
{
// ... using like standard file system
}

Remember to use using statement or call Dispose method after usage.

By default - the same as in SSH.NET itself - the server’s host key is trusted without verification. To pin an expected key, pass a SftpConnectionConfig with ExpectedHostKeyFingerprintSha256 set to the fingerprint reported by the server (e.g. via ssh-keyscan or ssh -o FingerprintHash=sha256); the connection is rejected if the presented key doesn’t match:

var connectionConfig = new SftpConnectionConfig(address, username, password)
{
ExpectedHostKeyFingerprintSha256 = "ohD8VZEXGWo6Ez8GSEJQ9WpafgLFsOfLOtGGQCQo6Og",
ConnectionTimeout = TimeSpan.FromSeconds(10)
};
using(FileSystem fileSystem = TagBites.IO.Sftp.SftpFileSystem.Create(connectionConfig))
{
// ... using like standard file system
}
  • The SFTP protocol has no server-side recursive listing, so recursive listing is performed by walking subdirectories one by one instead of a single call.
  • Direct stream access (OpenRead/OpenWrite) is read-only, same as the FTP file system.
  • Like FTP, S3, Dropbox and Google Cloud Storage, this file system does not support real change watching - see change notifications and watching for what CreateWatcher() does and doesn’t detect here.

Full implementation on github: TagBites.IO.Sftp.