使用库连接到远程服务器并复制文件。我的过程运行得很好,但是有一些小的事情我似乎无法解决,因为库的文档相当薄。
我有两个例行公事在工作。一个使用Tamir.SharpSsh类,另一个使用Tamir.SharpSsh.jsch类。
使用
Dim c As ChannelSftp Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile) Dim cnt As Integer = vct.Count
当一个或多个文件存在时,我得到一个计数,没有问题。当没有文件时,就会引发异常。
总之,我的日常工作只是一些小事情,我需要帮助。
tia AGP
发布于 2010-09-23 11:56:44
您可以使用要检查存在的文件的路径调用Tamir.SharpSsh.Sftp.GetFile方法(例如,在C#中,对不起):
private bool FileExists(string filePath)
{
try
{
SftpConnection connection = new SftpConnection(_host, _username, _password);
connection.Connect(_port);
connection.Get(filePath, _toDir);
}
catch(JSchException)
{
return false;
}
return true;
}在使用这个库时,我还注意到了其他一些问题--比如缺少GetFileInfo方法或递归的Gets和Puts。但总的来说,它可以完成任务。
简单的事实是,Tamir.SharpSsh不能远程重命名一个文件--它只是没有实现该功能。您可以购买一个更好的具有更多功能的库,例如:
Library
或者您可以扩展SharpSsh,因为它是开源的。
发布于 2010-08-23 20:07:31
您的问题是由于SFTP协议的限制。-要检查文件是否存在,请尝试返回该文件的属性;-大多数服务器目前不支持文件重命名。
发布于 2010-09-27 17:52:31
是的,我尝试了一些类似的Tamir.SharpSsh.jsch,但我觉得奇怪的是,您必须捕获异常才能检测到文件的不存在。以下是我发布后所做的事情:
Private Function FileExistsOnServer(ByVal c As ChannelSftp, ByVal sRemoteFile As String) As Boolean
Try
'get a file listing of the file
Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile)
Dim cnt As Integer = vct.Count
'if the count is greater than zero then the file already exists. if its 0 then the file does
'not exist on the server
If cnt > 0 Then
Return True
Else
Return False
End If
Catch ex As Exception
'if we get an exception then assume the file does not exist on the server
Return False
End Try
End Functionhttps://stackoverflow.com/questions/3548791
复制相似问题