[英]Check if file exist on SFTP remote server
以下代码将从SFTP remote server
下载名为file.txt
的文件到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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.