我必須讀取文件內容或讀取文件字節並需要在其中存儲我們的數據庫。如何在WinSCP .NET程序集中讀取遠程文件到內存
使用.NET System.IO.File
我們可以簡單地調用File.ReadAllBytes(file)
。
如何在WinSCP .NET程序集中使用RemoteFileInfo
?
我必須讀取文件內容或讀取文件字節並需要在其中存儲我們的數據庫。如何在WinSCP .NET程序集中讀取遠程文件到內存
使用.NET System.IO.File
我們可以簡單地調用File.ReadAllBytes(file)
。
如何在WinSCP .NET程序集中使用RemoteFileInfo
?
WinSCP .NET assembly不支持將遠程文件內容「下載」到內存中。
你所能做的只是把download the file放到一個本地的臨時位置,然後從那裏讀到內存中。
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Download to a temporary folder
string localPath = Path.GetTempFileName();
session.GetFiles(remotePath, localPath).Check();
// Read the file contents
byte[] contents = File.ReadAllBytes(localPath);
// Delete the temporary file
File.Delete(localPath);
}
感謝您的回覆...它的迭代操作 –
當我使用上面的appraoch時,它給我下面的錯誤無法獲取文件的屬性'WinSCP.RemoteDirectoryInfo 」。 –
在什麼聲明上? 'remotePath'必須是字符串路徑。你可能傳遞了一些無意義的價值。我的代碼中沒有涉及'RemoteDirectoryInfo'。它必須來自您自己的其他代碼。 –
問題是如何使用SSH在遠程服務器上打開文件? ...因爲AFAIK SCP代表安全複製,我不知道你想如何使用程序打開文件 – Carsten
我最好的猜測是你想使用類似[SSH.NET](https:// sshnet )(這應該提供SCP的功能) – Carsten