The concept of file sharing is an option when a file is opened successfully. The option defines what is to happen if another process tries to open the same file should it work or fail.
In the .NET framework, the option is defined by the %T:System.IO.FileShare% enumeration. The Xceed.FileSystem.AbstractFile uses the enumeration with the OpenRead() and OpenWrite() methods as an optional parameter. Default values are available and can be changed at a global level.
In the world of SFtp, FileShare options are only supported in versions 5 and later of the SFtp protocol. When lower versions are used, like the very common version 3 that most servers implement, the FileShare values used by SFtpFile and silently ignored when opening remote files.
The default behavior of the component is allow sharing of files for both reading and writing. This means that no blocking is requested when opening remote SFtp files. This behavior is different than other FileSystem media like DiskFile for example where no sharing is allowed when opening a file for writing.
Some SFtp servers return errors when %T:System.IO.FileShare% options block reading and/or writing by other server processes when a file is opened. To prevent errors, blocking is not used by default by SFtpFile.
Uploading opens SFtp remote files for writing and so uses the SFtpFile.DefaultAutomaticWriteFileShare property value.
If supported by the server, it is best practice to enable blocking for reading, writing and deleting when opening a file for writing. This way, it can be guaranteed that only a single process can write to a file at the same time. This is done by calling SFtpFile.SetDefaultAutomaticWriteFileShare method to %FileShare.None:System.IO.FileShareFileShare%.
The default automatic FileShare can be returned to its default value by calling the SFtpFile.SetDefaultAutomaticWriteFileShare method with null.
Downloading opens SFtp remote files for reading and so uses the SFtpFile.DefaultAutomaticReadFileShare property value.
If supported by the server, it is best practice to enable blocking for writing and deleting when opening a file for reading. This way, it can be guaranteed that only a single process can write to a file at the same time but allow for any number of processes to read from the file at the same time. This is done by setting the SFtpFile.DefaultAutomaticReadFileShare property to %FileShare.Read:System.IO.FileShareFileShare%.
The default automatic FileShare can be returned to its default value by calling the SFtpFile.SetDefaultAutomaticReadFileShare method with null.
The following example shows how to change the default FileShare option values. It also shows what happens when a FileShare value isn't supported by the SFtp server and what an application can do to remedy the situation.