Package Description
$ dotnet add package Anixe.IO.FtpClientPackage allows you to quickly integrate your .net application with ftp/sftp server.
Add into csproj as a package reference
<PackageReference Include="Anixe.IO.FtpClient" Version="2.0.0" />
Register package with DI container
public void AddFtpClient(IServiceCollection services)
{
services.AddFtpClient();
}
This package provides interface IFtpClient, resolve it.
// Ftp
var ftpConnection = _ftpClient.CreateFtpConnection("localhost", "user", "password", port: 21);
// OR
var ftpConnection = _ftpClient.CreateFtpConnection(
new ConnectionConfiguration("localhost", "user", "password", port), (nativeClient) => {});
// Sftp
var sftpConnection = _ftpClient.CreateSftpConnection("localhost", "user", "password", port: 22);
// OR
var sftpConnection = _ftpClient.CreateSftpConnection(
new ConnectionConfiguration("localhost", "user", "password", port), (nativeClient) => {});
Task<Result> downloadResult = anyConnection.DownloadFileAsync(localPath, remotePath, token);
Task<Result> uploadResult = anyConnection.UploadFileAsync(localPath, remotePath, token);
Task<Result<Stream>> openReadStreamResult = anyConnection.OpenReadAsync(remotePath, token);
Task<Result<Stream>> openWriteStreamResult = anyConnection.OpenWriteAsync(remotePath, token);
// Write (upload) example
var openWriteResult = await anyConnection.OpenWriteAsync("test.txt", CancellationToken.None);
try
{
const int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
int readBytes = 0;
var stream = System.IO.File.OpenRead(@"C:\file.txt");
do
{
readBytes = stream.Read(buffer, 0, bufferLength);
openWriteResult.Value.Write(buffer, 0, readBytes);
}
while (readBytes != 0);
}
catch (Exception e)
{
}
finally
{
openWriteResult.Value.Dispose();
}