Add SftpFileStream class

Add Sftp file methods to SftpClient class, similar to File class methods
This commit is contained in:
olegkap_cp
2011-06-07 18:50:08 +00:00
parent 3156167c0d
commit 4d79078a70
18 changed files with 2251 additions and 26 deletions
@@ -22,7 +22,7 @@ namespace Renci.SshNet.Common
/// </summary>
/// <param name="host">The host.</param>
/// <param name="port">The port.</param>
public PortForwardEventArgs(string host, uint port)
internal PortForwardEventArgs(string host, uint port)
{
if (host == null)
throw new ArgumentNullException("host");
@@ -12,6 +12,9 @@ namespace Renci.SshNet.Common
[Serializable]
public class SshAuthenticationException : SshException
{
/// <summary>
/// Initializes a new instance of the <see cref="SshAuthenticationException"/> class.
/// </summary>
public SshAuthenticationException()
{
@@ -128,7 +128,13 @@
<Compile Include="Security\Cryptography\Modes\OfbMode.cs" />
<Compile Include="Security\Cryptography\TransformMode.cs" />
<Compile Include="Security\Cryptography\Ciphers\DesCipher.cs" />
<Compile Include="Sftp\CloseCommand.cs" />
<Compile Include="Sftp\OpenCommand.cs" />
<Compile Include="Sftp\ReadCommand.cs" />
<Compile Include="Sftp\SetFileStatusCommand.cs" />
<Compile Include="Sftp\SetStatusCommand.cs" />
<Compile Include="Sftp\SftpFileAttributes.cs" />
<Compile Include="Sftp\SftpFileStream.cs" />
<Compile Include="Sftp\StatusCommand.cs" />
<Compile Include="Sftp\SymbolicLinkCommand.cs" />
<Compile Include="Sftp\Messages\SftpRequestMessage.cs" />
@@ -162,6 +168,7 @@
<Compile Include="Sftp\SftpSession.cs" />
<Compile Include="Sftp\UploadFileCommand.cs" />
<Compile Include="BaseClient.cs" />
<Compile Include="Sftp\WriteCommand.cs" />
<Compile Include="SshCommand.cs" />
<Compile Include="MessageEventArgs.cs" />
<Compile Include="ChannelAsyncResult.cs" />
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.SshNet.Sftp.Messages;
namespace Renci.SshNet.Sftp
{
internal class CloseCommand : SftpCommand
{
private byte[] _handle;
public CloseCommand(SftpSession sftpSession, byte[] handle)
: base(sftpSession)
{
this._handle = handle;
}
protected override void OnExecute()
{
this.SendCloseMessage(this._handle);
}
protected override void OnStatus(StatusCodes statusCode, string errorMessage, string language)
{
base.OnStatus(statusCode, errorMessage, language);
if (statusCode == StatusCodes.Ok)
{
this.CompleteExecution();
}
}
}
}
@@ -2,6 +2,7 @@
{
internal enum Flags
{
None = 0x00000000,
/// <summary>
/// SSH_FXF_READ
/// </summary>
@@ -56,9 +56,11 @@ namespace Renci.SshNet.Sftp.Messages
var flag = this.ReadUInt32();
if ((flag & 0x00000001) == 0x00000001) // SSH_FILEXFER_ATTR_SIZE
{
attributes.Size = this.ReadUInt64();
}
if ((flag & 0x00000002) == 0x00000002) // SSH_FILEXFER_ATTR_UIDGID
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.SshNet.Sftp.Messages;
using Renci.SshNet.Common;
using System.Globalization;
namespace Renci.SshNet.Sftp
{
internal class OpenCommand : SftpCommand
{
private string _path;
private Flags _flags;
public byte[] Handle { get; private set; }
public OpenCommand(SftpSession sftpSession, string path, Flags flags)
: base(sftpSession)
{
this._path = path;
this._flags = flags;
}
protected override void OnExecute()
{
this.SendOpenMessage(this._path, this._flags);
}
protected override void OnHandle(byte[] handle)
{
base.OnHandle(handle);
this.Handle = handle;
this.CompleteExecution();
}
protected override void OnStatus(StatusCodes statusCode, string errorMessage, string language)
{
base.OnStatus(statusCode, errorMessage, language);
if (statusCode == StatusCodes.NoSuchFile)
{
throw new SshFileNotFoundException(string.Format(CultureInfo.CurrentCulture, "Path '{0}' is not found.", this._path));
}
}
}
}
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.SshNet.Sftp.Messages;
using System.IO;
namespace Renci.SshNet.Sftp
{
internal class ReadCommand : SftpCommand
{
private byte[] _handle;
private ulong _offset;
private MemoryStream _output;
public byte[] Data { get; private set; }
public ReadCommand(SftpSession sftpSession, byte[] handle, ulong offset, uint bufferSize)
: base(sftpSession)
{
this._handle = handle;
this._offset = offset;
this._output = new MemoryStream((int)bufferSize);
}
protected override void OnExecute()
{
this.SendReadMessage(this._handle, this._offset, (uint)this._output.Capacity);
}
protected override void OnStatus(StatusCodes statusCode, string errorMessage, string language)
{
base.OnStatus(statusCode, errorMessage, language);
if (statusCode == StatusCodes.Eof)
{
this.Data = this._output.ToArray();
this.CompleteExecution();
}
}
protected override void OnData(byte[] data, bool isEof)
{
base.OnData(data, isEof);
this._output.Write(data, 0, data.Length);
this._output.Flush();
this._offset += (ulong)data.Length;
this.AsyncResult.DownloadedBytes = this._offset;
uint bytesLeft = (uint)(this._output.Capacity - this._output.Position);
this.SendReadMessage(this._handle, this._offset, bytesLeft);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
if (this._output != null)
{
this._output.Dispose();
this._output = null;
}
}
}
}
}
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.SshNet.Sftp.Messages;
namespace Renci.SshNet.Sftp
{
internal class SetStatusCommand : SftpCommand
{
private string _path;
private byte[] _handle;
private SftpFileAttributes _attributes;
public SetStatusCommand(SftpSession sftpSession, string path, SftpFileAttributes attributes)
: base(sftpSession)
{
this._path = path;
this._attributes = attributes;
}
public SetStatusCommand(SftpSession sftpSession, byte[] handle, SftpFileAttributes attributes)
: base(sftpSession)
{
this._handle = handle;
this._attributes = attributes;
}
protected override void OnExecute()
{
if (this._handle != null)
{
this.SendSetStatMessage(this._handle, this._attributes);
}
else if (this._path != null)
{
this.SendSetStatMessage(this._path, this._attributes);
}
}
protected override void OnStatus(StatusCodes statusCode, string errorMessage, string language)
{
base.OnStatus(statusCode, errorMessage, language);
if (statusCode == StatusCodes.Ok)
{
this.CompleteExecution();
}
}
}
}
@@ -81,6 +81,9 @@ namespace Renci.SshNet.Sftp
Task.Factory.StartNew(() => { this._asyncCallback(this); });
}
/// <summary>
/// Ends asynchronous operation invocation.
/// </summary>
public void EndInvoke()
{
// This method assumes that only 1 thread calls EndInvoke
@@ -125,7 +125,7 @@ namespace Renci.SshNet.Sftp
this.SendMessage(new SetStatMessage(this.SftpSession.NextRequestId, path, attributes));
}
protected void SendFSetStatMessage(byte[] handle, SftpFileAttributes attributes)
protected void SendSetStatMessage(byte[] handle, SftpFileAttributes attributes)
{
this.SendMessage(new FSetStatMessage(this.SftpSession.NextRequestId, handle, attributes));
}
@@ -165,6 +165,11 @@ namespace Renci.SshNet.Sftp
this.SendMessage(new StatMessage(this.SftpSession.NextRequestId, path));
}
protected void SendStatMessage(byte[] handle)
{
this.SendMessage(new FStatMessage(this.SftpSession.NextRequestId, handle));
}
protected void SendRenameMessage(string oldPath, string newPath)
{
this.SendMessage(new RenameMessage(this.SftpSession.NextRequestId, oldPath, newPath));
@@ -71,6 +71,9 @@ namespace Renci.SshNet.Sftp
/// <param name="attributes">Attributes of the directory or file.</param>
internal SftpFile(SftpSession sftpSession, string fullName, SftpFileAttributes attributes)
{
if (attributes == null)
throw new ArgumentNullException("attributes");
this._sftpSession = sftpSession;
this.Name = fullName.Substring(fullName.LastIndexOf('/') + 1);
@@ -0,0 +1,358 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Renci.SshNet.Sftp
{
public class SftpFileAttributes1
{
#region Bitmask constats
private static UInt32 S_IFMT = 0xF000; // bitmask for the file type bitfields
private static UInt32 S_IFSOCK = 0xC000; // socket
private static UInt32 S_IFLNK = 0xA000; // symbolic link
private static UInt32 S_IFREG = 0x8000; // regular file
private static UInt32 S_IFBLK = 0x6000; // block device
private static UInt32 S_IFDIR = 0x4000; // directory
private static UInt32 S_IFCHR = 0x2000; // character device
private static UInt32 S_IFIFO = 0x1000; // FIFO
private static UInt32 S_ISUID = 0x0800; // set UID bit
private static UInt32 S_ISGID = 0x0400; // set-group-ID bit (see below)
private static UInt32 S_ISVTX = 0x0200; // sticky bit (see below)
private static UInt32 S_IRUSR = 0x0100; // owner has read permission
private static UInt32 S_IWUSR = 0x0080; // owner has write permission
private static UInt32 S_IXUSR = 0x0040; // owner has execute permission
private static UInt32 S_IRGRP = 0x0020; // group has read permission
private static UInt32 S_IWGRP = 0x0010; // group has write permission
private static UInt32 S_IXGRP = 0x0008; // group has execute permission
private static UInt32 S_IROTH = 0x0004; // others have read permission
private static UInt32 S_IWOTH = 0x0002; // others have write permission
private static UInt32 S_IXOTH = 0x0001; // others have execute permission
#endregion
private bool _isBitFiledsBitSet;
private bool _isUIDBitSet;
private bool _isGroupIDBitSet;
private bool _isStickyBitSet;
/// <summary>
/// Gets or sets the time the current file or directory was last accessed.
/// </summary>
/// <value>
/// The time that the current file or directory was last accessed.
/// </value>
public DateTime LastAccessTime { get; private set; }
/// <summary>
/// Gets or sets the time when the current file or directory was last written to.
/// </summary>
/// <value>
/// The time the current file was last written.
/// </value>
public DateTime LastWriteTime { get; private set; }
/// <summary>
/// Gets or sets the size, in bytes, of the current file.
/// </summary>
/// <value>
/// The size of the current file in bytes.
/// </value>
public long Size { get; private set; }
/// <summary>
/// Gets or sets file user id.
/// </summary>
/// <value>
/// File user id.
/// </value>
public int UserId { get; set; }
/// <summary>
/// Gets or sets file group id.
/// </summary>
/// <value>
/// File group id.
/// </value>
public int GroupId { get; set; }
/// <summary>
/// Gets a value indicating whether file represents a socket.
/// </summary>
/// <value>
/// <c>true</c> if file represents a socket; otherwise, <c>false</c>.
/// </value>
public bool IsSocket { get; private set; }
/// <summary>
/// Gets a value indicating whether file represents a symbolic link.
/// </summary>
/// <value>
/// <c>true</c> if file represents a symbolic link; otherwise, <c>false</c>.
/// </value>
public bool IsSymbolicLink { get; private set; }
/// <summary>
/// Gets a value indicating whether file represents a regular file.
/// </summary>
/// <value>
/// <c>true</c> if file represents a regular file; otherwise, <c>false</c>.
/// </value>
public bool IsRegularFile { get; private set; }
/// <summary>
/// Gets a value indicating whether file represents a block device.
/// </summary>
/// <value>
/// <c>true</c> if file represents a block device; otherwise, <c>false</c>.
/// </value>
public bool IsBlockDevice { get; private set; }
/// <summary>
/// Gets a value indicating whether file represents a directory.
/// </summary>
/// <value>
/// <c>true</c> if file represents a directory; otherwise, <c>false</c>.
/// </value>
public bool IsDirectory { get; private set; }
/// <summary>
/// Gets a value indicating whether file represents a character device.
/// </summary>
/// <value>
/// <c>true</c> if file represents a character device; otherwise, <c>false</c>.
/// </value>
public bool IsCharacterDevice { get; private set; }
/// <summary>
/// Gets a value indicating whether file represents a named pipe.
/// </summary>
/// <value>
/// <c>true</c> if file represents a named pipe; otherwise, <c>false</c>.
/// </value>
public bool IsNamedPipe { get; private set; }
/// <summary>
/// Gets a value indicating whether the owner can read from this file.
/// </summary>
/// <value>
/// <c>true</c> if owner can read from this file; otherwise, <c>false</c>.
/// </value>
public bool OwnerCanRead { get; set; }
/// <summary>
/// Gets a value indicating whether the owner can write into this file.
/// </summary>
/// <value>
/// <c>true</c> if owner can write into this file; otherwise, <c>false</c>.
/// </value>
public bool OwnerCanWrite { get; set; }
/// <summary>
/// Gets a value indicating whether the owner can execute this file.
/// </summary>
/// <value>
/// <c>true</c> if owner can execute this file; otherwise, <c>false</c>.
/// </value>
public bool OwnerCanExecute { get; set; }
/// <summary>
/// Gets a value indicating whether the group members can read from this file.
/// </summary>
/// <value>
/// <c>true</c> if group members can read from this file; otherwise, <c>false</c>.
/// </value>
public bool GroupCanRead { get; set; }
/// <summary>
/// Gets a value indicating whether the group members can write into this file.
/// </summary>
/// <value>
/// <c>true</c> if group members can write into this file; otherwise, <c>false</c>.
/// </value>
public bool GroupCanWrite { get; set; }
/// <summary>
/// Gets a value indicating whether the group members can execute this file.
/// </summary>
/// <value>
/// <c>true</c> if group members can execute this file; otherwise, <c>false</c>.
/// </value>
public bool GroupCanExecute { get; set; }
/// <summary>
/// Gets a value indicating whether the others can read from this file.
/// </summary>
/// <value>
/// <c>true</c> if others can read from this file; otherwise, <c>false</c>.
/// </value>
public bool OthersCanRead { get; set; }
/// <summary>
/// Gets a value indicating whether the others can write into this file.
/// </summary>
/// <value>
/// <c>true</c> if others can write into this file; otherwise, <c>false</c>.
/// </value>
public bool OthersCanWrite { get; set; }
/// <summary>
/// Gets a value indicating whether the others can execute this file.
/// </summary>
/// <value>
/// <c>true</c> if others can execute this file; otherwise, <c>false</c>.
/// </value>
public bool OthersCanExecute { get; set; }
/// <summary>
/// Gets or sets the extensions.
/// </summary>
/// <value>
/// The extensions.
/// </value>
public IDictionary<string, string> Extensions { get; private set; }
internal uint Permissions
{
get
{
uint permission = 0;
if (this._isBitFiledsBitSet)
permission = permission | S_IFMT;
if (this.IsSocket)
permission = permission | S_IFSOCK;
if (this.IsSymbolicLink)
permission = permission | S_IFLNK;
if (this.IsRegularFile)
permission = permission | S_IFREG;
if (this.IsBlockDevice)
permission = permission | S_IFBLK;
if (this.IsDirectory)
permission = permission | S_IFDIR;
if (this.IsCharacterDevice)
permission = permission | S_IFCHR;
if (this.IsNamedPipe)
permission = permission | S_IFIFO;
if (this._isUIDBitSet)
permission = permission | S_ISUID;
if (this._isGroupIDBitSet)
permission = permission | S_ISGID;
if (this._isStickyBitSet)
permission = permission | S_ISVTX;
if (this.OwnerCanRead)
permission = permission | S_IRUSR;
if (this.OwnerCanWrite)
permission = permission | S_IWUSR;
if (this.OwnerCanExecute)
permission = permission | S_IXUSR;
if (this.GroupCanRead)
permission = permission | S_IRGRP;
if (this.GroupCanWrite)
permission = permission | S_IWGRP;
if (this.GroupCanExecute)
permission = permission | S_IXGRP;
if (this.OthersCanRead)
permission = permission | S_IROTH;
if (this.OthersCanWrite)
permission = permission | S_IWOTH;
if (this.OthersCanExecute)
permission = permission | S_IXOTH;
return permission;
}
private set
{
this._isBitFiledsBitSet = ((value & S_IFMT) == S_IFMT);
this.IsSocket = ((value & S_IFSOCK) == S_IFSOCK);
this.IsSymbolicLink = ((value & S_IFLNK) == S_IFLNK);
this.IsRegularFile = ((value & S_IFREG) == S_IFREG);
this.IsBlockDevice = ((value & S_IFBLK) == S_IFBLK);
this.IsDirectory = ((value & S_IFDIR) == S_IFDIR);
this.IsCharacterDevice = ((value & S_IFCHR) == S_IFCHR);
this.IsNamedPipe = ((value & S_IFIFO) == S_IFIFO);
this._isUIDBitSet = ((value & S_ISUID) == S_ISUID);
this._isGroupIDBitSet = ((value & S_ISGID) == S_ISGID);
this._isStickyBitSet = ((value & S_ISVTX) == S_ISVTX);
this.OwnerCanRead = ((value & S_IRUSR) == S_IRUSR);
this.OwnerCanWrite = ((value & S_IWUSR) == S_IWUSR);
this.OwnerCanExecute = ((value & S_IXUSR) == S_IXUSR);
this.GroupCanRead = ((value & S_IRGRP) == S_IRGRP);
this.GroupCanWrite = ((value & S_IWGRP) == S_IWGRP);
this.GroupCanExecute = ((value & S_IXGRP) == S_IXGRP);
this.OthersCanRead = ((value & S_IROTH) == S_IROTH);
this.OthersCanWrite = ((value & S_IWOTH) == S_IWOTH);
this.OthersCanExecute = ((value & S_IXOTH) == S_IXOTH);
}
}
internal SftpFileAttributes1(DateTime lastAccessTime, DateTime lastWriteTime, long size, int userId, int groupId, uint permissions)
{
this.LastAccessTime = lastAccessTime;
this.LastWriteTime = lastWriteTime;
this.Size = size;
this.UserId = userId;
this.GroupId = groupId;
this.Permissions = permissions;
}
}
}
File diff suppressed because it is too large Load Diff
@@ -156,20 +156,69 @@ namespace Renci.SshNet.Sftp
}
}
/// <summary>
/// Gets the file reference from the server.
/// </summary>
/// <param name="path">The path.</param>
/// <returns></returns>
public SftpFile GetSftpFile(string path)
public byte[] OpenFile(string path, Flags flags)
{
using (var cmd = new RealPathCommand(this, path))
using (var cmd = new OpenCommand(this, path, flags))
{
cmd.CommandTimeout = this._operationTimeout;
cmd.Execute();
return cmd.Files.FirstOrDefault();
return cmd.Handle;
}
}
public void CloseHandle(byte[] handle)
{
using (var cmd = new CloseCommand(this, handle))
{
cmd.CommandTimeout = this._operationTimeout;
cmd.Execute();
}
}
public void Write(byte[] handle, ulong offset, byte[] data)
{
using (var cmd = new WriteCommand(this, handle, offset, data))
{
cmd.CommandTimeout = this._operationTimeout;
cmd.Execute();
}
}
public byte[] Read(byte[] handle, ulong offset, uint length)
{
using (var cmd = new ReadCommand(this, handle, offset, length))
{
cmd.CommandTimeout = this._operationTimeout;
cmd.Execute();
return cmd.Data;
}
}
public SftpFileAttributes GetFileAttributes(byte[] handle)
{
using (var cmd = new StatusCommand(this, handle))
{
cmd.CommandTimeout = this._operationTimeout;
cmd.Execute();
return cmd.Attributes;
}
}
public void SetFileAttributes(byte[] handle, SftpFileAttributes attributes)
{
using (var cmd = new SetStatusCommand(this, handle, attributes))
{
cmd.CommandTimeout = this._operationTimeout;
cmd.Execute();
}
}
@@ -6,8 +6,9 @@ namespace Renci.SshNet.Sftp
internal class StatusCommand : SftpCommand
{
private string _path;
private byte[] _handle;
public SftpFile File { get; private set; }
public SftpFileAttributes Attributes { get; private set; }
public StatusCommand(SftpSession sftpSession, string path)
: base(sftpSession)
@@ -15,16 +16,29 @@ namespace Renci.SshNet.Sftp
this._path = path;
}
public StatusCommand(SftpSession sftpSession, byte[] handle)
: base(sftpSession)
{
this._handle = handle;
}
protected override void OnExecute()
{
this.SendStatMessage(this._path);
if (this._handle != null)
{
this.SendStatMessage(this._handle);
}
else if (this._path != null)
{
this.SendStatMessage(this._path);
}
}
protected override void OnAttributes(SftpFileAttributes attributes)
{
base.OnAttributes(attributes);
this.File = new SftpFile(this.SftpSession, this._path, attributes);
this.Attributes = attributes;
this.CompleteExecution();
}
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.SshNet.Sftp.Messages;
namespace Renci.SshNet.Sftp
{
internal class WriteCommand : SftpCommand
{
private byte[] _handle;
private ulong _offset;
private byte[] _data;
public WriteCommand(SftpSession sftpSession, byte[] handle, ulong offset, byte[] data)
: base(sftpSession)
{
this._handle = handle;
this._offset = offset;
this._data = data;
}
protected override void OnExecute()
{
this.SendWriteMessage(this._handle, this._offset, this._data);
}
protected override void OnStatus(StatusCodes statusCode, string errorMessage, string language)
{
base.OnStatus(statusCode, errorMessage, language);
if (statusCode == StatusCodes.Ok)
{
this.CompleteExecution();
}
}
}
}
+520 -13
View File
@@ -3,6 +3,8 @@ using System.Linq;
using System.Collections.Generic;
using System.IO;
using Renci.SshNet.Sftp;
using System.Text;
using Renci.SshNet.Common;
namespace Renci.SshNet
{
@@ -140,7 +142,7 @@ namespace Renci.SshNet
// Ensure that connection is established.
this.EnsureConnection();
var file = this._sftpSession.GetSftpFile(path);
var file = this.Get(path);
file.SetPermissions(mode);
}
@@ -161,11 +163,12 @@ namespace Renci.SshNet
var fullPath = this._sftpSession.GetCanonicalPath(path);
var cmd = new CreateDirectoryCommand(this._sftpSession, fullPath);
using (var cmd = new CreateDirectoryCommand(this._sftpSession, fullPath))
{
cmd.CommandTimeout = this.OperationTimeout;
cmd.CommandTimeout = this.OperationTimeout;
cmd.Execute();
cmd.Execute();
}
}
/// <summary>
@@ -360,18 +363,23 @@ namespace Renci.SshNet
if (path == null)
throw new ArgumentNullException("path");
// Ensure that connection is established.
this.EnsureConnection();
var fullPath = this._sftpSession.GetCanonicalPath(path);
var cmd = new StatusCommand(this._sftpSession, fullPath);
using (var cmd = new StatusCommand(this._sftpSession, fullPath))
{
cmd.CommandTimeout = this.OperationTimeout;
cmd.CommandTimeout = this.OperationTimeout;
cmd.Execute();
cmd.Execute();
return cmd.File;
if (cmd.Attributes == null)
{
return null;
}
else
{
return new SftpFile(this._sftpSession, fullPath, cmd.Attributes);
}
}
}
/// <summary>
@@ -540,6 +548,505 @@ namespace Renci.SshNet
throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async method on this type, or EndUploadFile was called multiple times with the same IAsyncResult.");
}
#region File Methods
/// <summary>
/// Appends lines to a file, and then closes the file.
/// </summary>
/// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param>
/// <param name="contents">The lines to append to the file.</param>
public void AppendAllLines(string path, IEnumerable<string> contents)
{
using (var stream = this.AppendText(path))
{
foreach (var line in contents)
{
stream.WriteLine(line);
}
}
}
/// <summary>
/// Appends lines to a file by using a specified encoding, and then closes the file.
/// </summary>
/// <param name="path">The file to append the lines to. The file is created if it does not already exist.</param>
/// <param name="contents">The lines to append to the file.</param>
/// <param name="encoding">The character encoding to use.</param>
public void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
{
using (var stream = this.AppendText(path, encoding))
{
foreach (var line in contents)
{
stream.WriteLine(line);
}
}
}
/// <summary>
/// Opens a file, appends the specified string to the file, and then closes the file.
/// If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
/// </summary>
/// <param name="path">The file to append the specified string to.</param>
/// <param name="contents">The string to append to the file.</param>
public void AppendAllText(string path, string contents)
{
using (var stream = this.AppendText(path))
{
stream.Write(contents);
}
}
/// <summary>
/// Opens a file, appends the specified string to the file, and then closes the file.
/// If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
/// </summary>
/// <param name="path">The file to append the specified string to.</param>
/// <param name="contents">The string to append to the file.</param>
/// <param name="encoding">The character encoding to use.</param>
public void AppendAllText(string path, string contents, Encoding encoding)
{
using (var stream = this.AppendText(path, encoding))
{
stream.Write(contents);
}
}
/// <summary>
/// Creates a <see cref="System.IO.StreamWriter"/> that appends UTF-8 encoded text to an existing file.
/// </summary>
/// <param name="path">The path to the file to append to.</param>
/// <returns>A StreamWriter that appends UTF-8 encoded text to an existing file.</returns>
public StreamWriter AppendText(string path)
{
return this.AppendText(path, Encoding.UTF8);
}
/// <summary>
/// Creates a <see cref="System.IO.StreamWriter"/> that appends UTF-8 encoded text to an existing file.
/// </summary>
/// <param name="path">The path to the file to append to.</param>
/// <param name="encoding">The character encoding to use.</param>
/// <returns>
/// A StreamWriter that appends UTF-8 encoded text to an existing file.
/// </returns>
public StreamWriter AppendText(string path, Encoding encoding)
{
return new StreamWriter(new SftpFileStream(this._sftpSession, path, FileMode.Append, FileAccess.Write), encoding);
}
/// <summary>
/// Creates or overwrites a file in the specified path.
/// </summary>
/// <param name="path">The path and name of the file to create.</param>
/// <returns>A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path</returns>
public SftpFileStream Create(string path)
{
return new SftpFileStream(this._sftpSession, path, FileMode.Create, FileAccess.ReadWrite);
}
/// <summary>
/// Creates or overwrites the specified file.
/// </summary>
/// <param name="path">The path and name of the file to create.</param>
/// <param name="bufferSize">The number of bytes buffered for reads and writes to the file.</param>
/// <returns>A <see cref="SftpFileStream"/> that provides read/write access to the file specified in path</returns>
public SftpFileStream Create(string path, int bufferSize)
{
return new SftpFileStream(this._sftpSession, path, FileMode.Create, FileAccess.ReadWrite, bufferSize);
}
/// <summary>
/// Creates or opens a file for writing UTF-8 encoded text.
/// </summary>
/// <param name="path">The file to be opened for writing.</param>
/// <returns>A <see cref="System.IO.StreamWriter"/> that writes to the specified file using UTF-8 encoding.</returns>
public StreamWriter CreateText(string path)
{
return new StreamWriter(this.OpenWrite(path), Encoding.UTF8);
}
/// <summary>
/// Creates or opens a file for writing UTF-8 encoded text.
/// </summary>
/// <param name="path">The file to be opened for writing.</param>
/// <param name="encoding">The character encoding to use.</param>
/// <returns> A <see cref="System.IO.StreamWriter"/> that writes to the specified file using UTF-8 encoding. </returns>
public StreamWriter CreateText(string path, Encoding encoding)
{
return new StreamWriter(this.OpenWrite(path), encoding);
}
/// <summary>
/// Deletes the specified file or directory. An exception is not thrown if the specified file does not exist.
/// </summary>
/// <param name="path">The name of the file or directory to be deleted. Wildcard characters are not supported.</param>
public void Delete(string path)
{
var file = this.Get(path);
if (file == null)
{
throw new SshFileNotFoundException(path);
}
file.Delete();
}
/// <summary>
/// Determines whether the specified file exists.
/// </summary>
/// <param name="path">The file to check.</param>
/// <returns><c>true</c> if path contains the name of an existing file; otherwise, <c>false</c>.</returns>
public bool Exists(string path)
{
var file = this.Get(path);
return file != null;
}
/// <summary>
/// Gets the System.IO.FileAttributes of the file on the path.
/// </summary>
/// <param name="path">The path to the file.</param>
/// <returns>The System.IO.FileAttributes of the file on the path.</returns>
public FileAttributes GetAttributes(string path)
{
throw new NotImplementedException();
}
/// <summary>
/// Returns the date and time the specified file or directory was last accessed.
/// </summary>
/// <param name="path">The file or directory for which to obtain access date and time information.</param>
/// <returns>A <see cref="System.DateTime"/> structure set to the date and time that the specified file or directory was last accessed. This value is expressed in local time.</returns>
public DateTime GetLastAccessTime(string path)
{
var file = this.Get(path);
return file.LastAccessTime;
}
/// <summary>
/// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.
/// </summary>
/// <param name="path">The file or directory for which to obtain access date and time information.</param>
/// <returns>A <see cref="System.DateTime"/> structure set to the date and time that the specified file or directory was last accessed. This value is expressed in UTC time.</returns>
public DateTime GetLastAccessTimeUtc(string path)
{
var file = this.Get(path);
return file.LastAccessTime.ToUniversalTime();
}
/// <summary>
/// Returns the date and time the specified file or directory was last written to.
/// </summary>
/// <param name="path">The file or directory for which to obtain write date and time information.</param>
/// <returns>A <see cref="System.DateTime"/> structure set to the date and time that the specified file or directory was last written to. This value is expressed in local time.</returns>
public DateTime GetLastWriteTime(string path)
{
var file = this.Get(path);
return file.LastWriteTime;
}
/// <summary>
/// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.
/// </summary>
/// <param name="path">The file or directory for which to obtain write date and time information.</param>
/// <returns>A <see cref="System.DateTime"/> structure set to the date and time that the specified file or directory was last written to. This value is expressed in UTC time.</returns>
public DateTime GetLastWriteTimeUtc(string path)
{
var file = this.Get(path);
return file.LastWriteTime.ToUniversalTime();
}
/// <summary>
/// Opens a <see cref="SftpFileStream"/> on the specified path with read/write access.
/// </summary>
/// <param name="path">The file to open.</param>
/// <param name="mode">A <see cref="System.IO.FileMode"/> 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.</param>
/// <returns>An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and access.</returns>
public SftpFileStream Open(string path, FileMode mode)
{
return new SftpFileStream(this._sftpSession, path, mode, FileAccess.ReadWrite);
}
/// <summary>
/// Opens a <see cref="SftpFileStream"/> on the specified path, with the specified mode and access.
/// </summary>
/// <param name="path">The file to open.</param>
/// <param name="mode">A <see cref="System.IO.FileMode"/> 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.</param>
/// <param name="access">A <see cref="System.IO.FileAccess"/> value that specifies the operations that can be performed on the file.</param>
/// <returns>An unshared <see cref="SftpFileStream"/> that provides access to the specified file, with the specified mode and access.</returns>
public SftpFileStream Open(string path, FileMode mode, FileAccess access)
{
return new SftpFileStream(this._sftpSession, path, mode, access);
}
/// <summary>
/// Opens an existing file for reading.
/// </summary>
/// <param name="path">The file to be opened for reading.</param>
/// <returns>A read-only System.IO.FileStream on the specified path.</returns>
public SftpFileStream OpenRead(string path)
{
return new SftpFileStream(this._sftpSession, path, FileMode.Open, FileAccess.Read);
}
/// <summary>
/// Opens an existing UTF-8 encoded text file for reading.
/// </summary>
/// <param name="path">The file to be opened for reading.</param>
/// <returns>A <see cref="System.IO.StreamReader"/> on the specified path.</returns>
public StreamReader OpenText(string path)
{
return new StreamReader(this.OpenRead(path), Encoding.UTF8);
}
/// <summary>
/// Opens an existing file for writing.
/// </summary>
/// <param name="path">The file to be opened for writing.</param>
/// <returns>An unshared <see cref="SftpFileStream"/> object on the specified path with <see cref="System.IO.FileAccess.Write"/> access.</returns>
public SftpFileStream OpenWrite(string path)
{
return new SftpFileStream(this._sftpSession, path, FileMode.OpenOrCreate, FileAccess.Write);
}
/// <summary>
/// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <returns>A byte array containing the contents of the file.</returns>
public byte[] ReadAllBytes(string path)
{
using (var stream = this.OpenRead(path))
{
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
return buffer;
}
}
/// <summary>
/// Opens a text file, reads all lines of the file, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <returns>A string array containing all lines of the file.</returns>
public string[] ReadAllLines(string path)
{
return this.ReadAllLines(path, Encoding.UTF8);
}
/// <summary>
/// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <param name="encoding">The encoding applied to the contents of the file.</param>
/// <returns>A string array containing all lines of the file.</returns>
public string[] ReadAllLines(string path, Encoding encoding)
{
var lines = new List<string>();
using (var stream = new StreamReader(this.OpenRead(path), encoding))
{
while (!stream.EndOfStream)
{
lines.Add(stream.ReadLine());
}
}
return lines.ToArray();
}
/// <summary>
/// Opens a text file, reads all lines of the file, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <returns>A string containing all lines of the file.</returns>
public string ReadAllText(string path)
{
return this.ReadAllText(path, Encoding.UTF8);
}
/// <summary>
/// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <param name="encoding">The encoding applied to the contents of the file.</param>
/// <returns>A string containing all lines of the file.</returns>
public string ReadAllText(string path, Encoding encoding)
{
var lines = new List<string>();
using (var stream = new StreamReader(this.OpenRead(path), encoding))
{
return stream.ReadToEnd();
}
}
/// <summary>
/// Reads the lines of a file.
/// </summary>
/// <param name="path">The file to read.</param>
/// <returns>The lines of the file.</returns>
public IEnumerable<string> ReadLines(string path)
{
return this.ReadAllLines(path);
}
/// <summary>
/// Read the lines of a file that has a specified encoding.
/// </summary>
/// <param name="path">The file to read.</param>
/// <param name="encoding">The encoding that is applied to the contents of the file.</param>
/// <returns>The lines of the file.</returns>
public IEnumerable<string> ReadLines(string path, Encoding encoding)
{
return this.ReadAllLines(path, encoding);
}
/// <summary>
/// Sets the date and time the specified file was last accessed.
/// </summary>
/// <param name="path">The file for which to set the access date and time information.</param>
/// <param name="lastAccessTime">A <see cref="System.DateTime"/> containing the value to set for the last access date and time of path. This value is expressed in local time.</param>
public void SetLastAccessTime(string path, DateTime lastAccessTime)
{
throw new NotImplementedException();
}
/// <summary>
/// Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.
/// </summary>
/// <param name="path">The file for which to set the access date and time information.</param>
/// <param name="lastAccessTimeUtc">A <see cref="System.DateTime"/> containing the value to set for the last access date and time of path. This value is expressed in UTC time.</param>
public void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
{
throw new NotImplementedException();
}
/// <summary>
/// Sets the date and time that the specified file was last written to.
/// </summary>
/// <param name="path">The file for which to set the date and time information.</param>
/// <param name="lastWriteTime">A System.DateTime containing the value to set for the last write date and time of path. This value is expressed in local time.</param>
public void SetLastWriteTime(string path, DateTime lastWriteTime)
{
throw new NotImplementedException();
}
/// <summary>
/// Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to.
/// </summary>
/// <param name="path">The file for which to set the date and time information.</param>
/// <param name="lastWriteTimeUtc">A System.DateTime containing the value to set for the last write date and time of path. This value is expressed in UTC time.</param>
public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
{
throw new NotImplementedException();
}
/// <summary>
/// Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="bytes">The bytes to write to the file.</param>
public void WriteAllBytes(string path, byte[] bytes)
{
using (var stream = this.OpenWrite(path))
{
stream.Write(bytes, 0, bytes.Length);
}
}
/// <summary>
/// Creates a new file, writes a collection of strings to the file, and then closes the file.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="contents">The lines to write to the file.</param>
public void WriteAllLines(string path, IEnumerable<string> contents)
{
this.WriteAllLines(path, contents, Encoding.UTF8);
}
/// <summary>
/// Creates a new file, write the specified string array to the file, and then closes the file.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="contents">The string array to write to the file.</param>
public void WriteAllLines(string path, string[] contents)
{
this.WriteAllLines(path, contents, Encoding.UTF8);
}
/// <summary>
/// Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="contents">The lines to write to the file.</param>
/// <param name="encoding">The character encoding to use.</param>
public void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)
{
using (var stream = this.CreateText(path, encoding))
{
foreach (var line in contents)
{
stream.WriteLine(line);
}
}
}
/// <summary>
/// Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="contents">The string array to write to the file.</param>
/// <param name="encoding">An <see cref="System.Text.Encoding"/> object that represents the character encoding applied to the string array.</param>
public void WriteAllLines(string path, string[] contents, Encoding encoding)
{
using (var stream = this.CreateText(path, encoding))
{
foreach (var line in contents)
{
stream.WriteLine(line);
}
}
}
/// <summary>
/// Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="contents">The string to write to the file.</param>
public void WriteAllText(string path, string contents)
{
using (var stream = this.CreateText(path))
{
stream.Write(contents);
}
}
/// <summary>
/// Creates a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="contents">The string to write to the file.</param>
/// <param name="encoding">The encoding to apply to the string.</param>
public void WriteAllText(string path, string contents, Encoding encoding)
{
using (var stream = this.CreateText(path, encoding))
{
stream.Write(contents);
}
}
//public FileSecurity GetAccessControl(string path);
//public FileSecurity GetAccessControl(string path, AccessControlSections includeSections);
//public DateTime GetCreationTime(string path);
//public DateTime GetCreationTimeUtc(string path);
//public void SetAccessControl(string path, FileSecurity fileSecurity);
//public void SetAttributes(string path, FileAttributes fileAttributes);
//public void SetCreationTime(string path, DateTime creationTime);
//public void SetCreationTimeUtc(string path, DateTime creationTimeUtc);
#endregion
/// <summary>
/// Called when client is connected to the server.
/// </summary>