下面的代码会将名为的文件file.txt
从.SFTP remote server
local machine
sftp.Get("/usr/mine/file.txt" , "C:/Folder/");
我想要做的是检查文件是否file.txt
存在remote server
。我该怎么做这个检查。帮助
我在用SharpSSH
您可以考虑只接受小打击并尝试下载文件。如果它不存在,应该抛出一个异常,你可以抓住它并继续前进。检查文件是否存在是一个不稳定的情况,因此在大多数情况下,最好尝试执行您的操作。
这应该可以解决问题。
using (var sftp = new SftpClient(host, username, password))
{
try
{
sftp.Connect();
MessageBox.Show(sftp.Exists(remoteDirectory).ToString());
}
catch (Exception Sftpex)
{
MessageBox.Show(Sftpex.ToString());
}
}
我这样做是通过使用 .GetFileList 并将值读取到 ArrayList 中,然后循环遍历每个值,将文件名添加到列表框中。然后我根据列表框检查我的输入文件,看看它是否存在。下面的示例代码将 .GetFileList 值添加到 ArrayList 中,然后添加到列表框中。
顺便说一句 - 这是 VB.NET :)
Dim InputFileList As ArrayList = oSftp.GetFileList(frmOptions.tbFTPInboundFolder.Text)
For Each f In InputFileList
If f.ToString() <> "." AndAlso f.ToString <> ".." Then
frmMain.lbFTPInputDirectory.Items.Add(f)
End If
Next