【问题标题】:Read FTP file contents to a string in C# FluentFTP将 FTP 文件内容读取到 C# FluentFTP 中的字符串
【发布时间】:2021-11-11 18:31:32
【问题描述】:

我的类继承自 FluentFTP,我创建了一个这样的类。我需要在这个类中创建一个名为Read 的函数。读取函数的目的是通过逐行读取我从 FTP 读取的文件的内容,向我返回一个字符串。稍后我将处理旋转字符串。 FluentFTP 中有没有这样的方法? Ff 没有,我应该如何创建函数?

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);
    }
}

我不能这样写,因为我写的Client来自FTP。由于我在之前的代码中是从 SFTP 派生出来的,所以我想使用类似的代码 sn-p 来处理它,但是 FluentFTP 中没有这样的代码 sn-p。 Read函数中应该如何操作?

在另一个文件中,我想这样称呼它。

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);
}

【问题讨论】:

    标签: c# .net ftp sftp fluentftp


    【解决方案1】:

    要使用 FluentFTP 将文件读取到字符串,您可以使用 FtpClient.Download method 将文件内容写入 Streambyte[] 数组。以下示例使用后者。

    if (!client.Download(out byte[] bytes, "/remote/path/file.txt"))
    {
        throw new Exception("Cannot read file");
    }
    
    string contents = Encoding.UTF8.GetString(bytes);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多