My class inherits from FluentFTP and I have created a class like this. I need to create a function called Read
in this class. The purpose of the read function is to return a string to me by reading the contents of the files I have read from the FTP line by line. I'll process the rotating string later. Is there a method for this in FluentFTP? Ff there is none, how should I create the function?
using FluentFTP;
public class CustomFtpClient : FtpClient
{
public CustomFtpClient(
string host, int port, string username, string password) :
base(host, port, username, password)
{
Client = new FtpClient(host, port, username, password);
Client.AutoConnect();
}
private FtpClient Client { get; }
public string ReadFile(string remoteFileName)
{
Client.BufferSize = 4 * 1024;
return Client.ReadAllText(remoteFileName);
}
}
I can't write like this because the Client
I'm writing comes from FTP. Since I derived it from SFTP in my previous codes, I wanted to use a similar code snippet to it, but there is no such code snippet in FluentFTP. How should I perform an operation in the Read
function?
In another file, I want to call it like this.
CustomFtpClient = new CustomFtpClient(ftpurl, 21, ftpusername, ftppwd);
var listedfiles = CustomFtpClient.GetListing("inbound");
var onlyedifiles = listedfiles.Where(z =>
z.FullName.ToLower().Contains(".txt") || z.FullName.ToLower().Contains("940"))
.ToList();
foreach (var item in onlyedifiles)
{
//var filestr = CustomFtpClient.ReadFile(item.FullName);
}
To read file to a string using FluentFTP, you can use FtpClient.DownloadStream
or FtpClient.DownloadBytes
that either writes the file contents to Stream
or byte[]
array. The following examples uses the latter.
if (!client.Download(out byte[] bytes, "/remote/path/file.txt"))
{
throw new Exception("Cannot read file");
}
string contents = Encoding.UTF8.GetString(bytes);