如何在WinSCP .NET程序集中将远程文件读取到内存中

问题描述 投票:2回答:1

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

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

如何使用RemoteFileInfo在WinSCP .NET程序集中执行此操作?

.net winscp winscp-net
1个回答
2
投票

WinSCP .NET assembly不支持将远程文件内容“下载”到内存中。

您所能做的就是将download the file带到当地的临时位置,然后从那里读取内存。

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    string localPath = Path.GetTempFileName();

    try
    {
        // Download to a temporary folder
        session.GetFiles(remotePath, localPath).Check();

        // Read the file contents
        byte[] contents = File.ReadAllBytes(localPath);
    }
    finally
    {
        // Delete the temporary file
        File.Delete(localPath);
    }
}

在WinSCP跟踪器中有Stream interface in .NET assembly的请求。

© www.soinside.com 2019 - 2024. All rights reserved.