【问题标题】:How in WinSCP .NET assembly read remote file to memoryWinSCP .NET 程序集中如何将远程文件读取到内存
【发布时间】:2016-02-10 10:30:49
【问题描述】:

我必须读取文件内容或读取文件字节并且需要将其存储在我们的数据库中。

使用 .NET System.IO.File 我们可以简单地调用File.ReadAllBytes(file)

如何在 WinSCP .NET 程序集中使用RemoteFileInfo

【问题讨论】:

    标签: .net winscp winscp-net


    【解决方案1】:

    WinSCP .NET 程序集仅在当前 beta 版本 (5.18) 中支持使用流提供远程文件的内容,使用 Session.GetFile method

    using (Stream stream = session.GetFile("/path/file.ext"))
    {
        // use the stream
    }
    

    使用当前的稳定版本,您只能将download the file 到本地临时位置并从那里将其读取到内存中。

    using (Session session = new Session())
    {
        // Connect
        session.Open(sessionOptions);
         
        // Download to a temporary folder
        var transfer = session.GetFileToDirectory(remotePath, Path.GetTempPath());
        
        try
        {
            // Read the file contents
            byte[] contents = File.ReadAllBytes(transfer.Destination);
        }
        finally
        {
            // Delete the temporary file
            File.Delete(transfer.Destination);
        }
    }
    

    类似问题:Access remote file contents as a stream using WinSCP .NET assembly

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-05
      • 2021-12-29
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      • 2017-03-24
      • 2017-12-13
      • 2023-04-03
      相关资源
      最近更新 更多