Access remote file contents as a stream using WinSCP .NET assembly

放肆的年华 提交于 2019-11-30 22:13:11

The WinSCP .NET assembly Session API cannot not provide the contents of downloaded file using streams.

So all you can do, is to download the remote file to a local temporary location using the Session.GetFiles and read the file from there:

// Generate unique file name for the temporary file
string tempPath = Path.GetTempFileName();

// Download the remote file to the temporary location
session.GetFiles("/path/file.ext", tempPath).Check();

try
{
    // Open the temporarily downloaded file for reading
    using (Stream stream = File.OpenRead(tempPath))
    {
        // use the stream
        blockBlob.UploadFromStream(fileStream);
        blobUri = blockBlob.Uri.ToString();
    }
}
finally
{
    // Discard the temporarily downloaded file
    File.Delete(tempPath);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!