Renci.SshNet.SftpClient.Open C# (CSharp) Method

Open() public method

Opens a SftpFileStream on the specified path with read/write access.
is null. Client is not connected. The method was called after the client was disposed.
public Open ( string path, FileMode mode ) : SftpFileStream
path string The file to open.
mode FileMode A value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.
return SftpFileStream
        public SftpFileStream Open(string path, FileMode mode)
        {
            return Open(path, mode, FileAccess.ReadWrite);
        }

Same methods

SftpClient::Open ( string path, FileMode mode, FileAccess access ) : SftpFileStream

Usage Example

Beispiel #1
0
        /// <inheritdoc />
        public byte[]? DownloadFileFromServer(string basePath, string fileName)
        {
            if (String.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }
            if (String.IsNullOrWhiteSpace(basePath))
            {
                throw new ArgumentNullException(nameof(basePath));
            }

            return(_retryPolicy.Execute(() =>
            {
                EnsureConnected();

                var filePath = GetFileFullPath(basePath, fileName);

                if (!_sftpClient.Exists(filePath))
                {
                    _logger.LogWarning($"File \"{filePath}\" not found on SFTP server");
                    return null;
                }

                using (var file = _sftpClient.Open(filePath, FileMode.Open))
                {
                    using (var ms = new MemoryStream())
                    {
                        file.CopyTo(ms);
                        return ms.ToArray();
                    }
                }
            }));
        }