我必须读取文件内容或读取文件字节,并且需要将其存储在我们的数据库中。
使用 .NET System.IO.File
,我们可以简单地调用File.ReadAllBytes(file)
.
如何在 WinSCP .NET 程序集中使用RemoteFileInfo
?
我必须读取文件内容或读取文件字节,并且需要将其存储在我们的数据库中。
使用 .NET System.IO.File
,我们可以简单地调用File.ReadAllBytes(file)
.
如何在 WinSCP .NET 程序集中使用RemoteFileInfo
?
WinSCP .NET 程序集仅在当前 beta 版本 (5.18) 中支持使用流提供远程文件的内容,使用Session.GetFile
方法:
using (Stream stream = session.GetFile("/path/file.ext"))
{
// use the stream
}
使用当前的稳定版本,您只能将文件下载到本地临时位置并从那里将其读取到内存中。
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);
}
}