Table of Contents

Class SftpFileSystem

Namespace
SshSharp.IO.Abstractions
Assembly
SshSharp.IO.Abstractions.dll

An System.IO.Abstractions.IFileSystem (System.IO.Abstractions) over SFTP: code written against System.IO.Abstractions.IFileSystem instead of File, Directory and Path reads and writes files on an SSH server. Nothing is mounted, so only this application sees them.

public sealed class SftpFileSystem : IFileSystem, IDisposable, IAsyncDisposable
Inheritance
SftpFileSystem
Implements
IFileSystem
Inherited Members

Remarks

Paths are POSIX paths on the server, whatever OS this runs on: Path uses /, and relative paths are relative to the current directory (System.IO.Abstractions.IDirectory.GetCurrentDirectory()), which starts as the login directory and is kept here, not on the server. A \ is an ordinary character in a POSIX file name.

The abstraction is synchronous at heart: each synchronous call waits for its SFTP round trips, on the thread pool and bounded by OperationTimeout; the asynchronous methods (ReadAllTextAsync and so on) do not block. Errors are the exceptions System.IO throws: FileNotFoundException, DirectoryNotFoundException, UnauthorizedAccessException and IOException (also for a failed session or connection).

SFTP has no creation times (the creation time reads as the last write time, and setting it does nothing), no file system notifications, drives, version information, encryption or handles: FileSystemWatcher, DriveInfo, FileVersionInfo, RandomAccess and the SafeFileHandle overloads throw NotSupportedException.

Built from a session factory (SftpFileSystem(Func<CancellationToken, Task<SftpClient>>, SftpFileSystemOptions?)), the file system follows a connection that drops and comes back: it keeps one session while it works, and opens a new one after the session or its connection fails. Built from one SftpClient, it uses that session for its lifetime, and fails once it fails.

Constructors

SftpFileSystem(SftpClient, SftpFileSystemOptions?)

A file system over sftp.

SftpFileSystem(Func<CancellationToken, Task<SftpClient>>, SftpFileSystemOptions?)

A file system over sessions from sessionFactory: one is opened on the first call and shared by every call after it, and a new one is opened when the session or its connection fails. The file system owns the sessions it opens and disposes them (dispose the file system when it is no longer used).

Properties

Directory

Abstraction for static methods in Directory.

DirectoryInfo

A factory for the creation of wrappers for DirectoryInfo.

DriveInfo

Not supported: SFTP has no drives (every factory method throws NotSupportedException).

File

Abstraction for static methods in File.

FileInfo

A factory for the creation of wrappers for FileInfo.

FileStream

A factory for the creation of wrappers for FileStream.

FileSystemWatcher

Not supported: SFTP has no change notifications.

FileVersionInfo

Not supported: version resources would mean reading every file.

Path

POSIX path operations (/), for the server's paths.

RandomAccess

Not supported: it works on OS file handles, which remote files do not have.

Methods

Dispose()

Disposes the file system as DisposeAsync() does, waiting for it (on the thread pool, as synchronous calls do).

DisposeAsync()

Disposes the SFTP sessions the file system opened from its session factory; later calls throw ObjectDisposedException. A file system built from one SftpClient leaves that session alone: it stays the caller's, and this does nothing.