Vb.net SharpSSH进程和重命名
使用库连接到远程服务器并复制文件。我的流程运行得相当好,但有一些较小的问题,我似乎无法解决,因为库的文档相当薄 我有两个日常工作。一个使用Tamir.SharpSsh类,另一个使用Tamir.SharpSsh.jsch类Vb.net SharpSSH进程和重命名,vb.net,sftp,jsch,sharpssh,Vb.net,Sftp,Jsch,Sharpssh,使用库连接到远程服务器并复制文件。我的流程运行得相当好,但有一些较小的问题,我似乎无法解决,因为库的文档相当薄 我有两个日常工作。一个使用Tamir.SharpSsh类,另一个使用Tamir.SharpSsh.jsch类 使用Tamir.SharpSsh类,我能够将文件从本地服务器复制到远程服务器,并点击pogress事件。我不能做的是确定远程服务器上是否存在一个特定的文件,比如说/Report/data.txt。如果存在或不存在,我需要采取不同的行动。另外,如何重命名远程服务器上的文件。我试着
Dim c As ChannelSftp
Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile)
Dim cnt As Integer = vct.Count
AGP您的问题是由于SFTP协议的限制。 -要检查文件是否存在,请尝试返回该文件的属性;
-大多数服务器目前不支持文件重命名。您可以使用要检查存在的文件的路径调用
Tamir.SharpSsh.Sftp.GetFile
方法(例如C#,抱歉):
通过使用这个库,我还注意到了一些其他问题,比如缺少GetFileInfo
方法或递归get和put。但总的来说,它完成了任务
简单的事实是,Tamir.SharpSsh无法远程重命名文件-它只是没有实现该功能。您可以购买具有更多功能的更好的库,例如:
- Kellerman软件.NET SFTP库
- wodSFTP.NET
- NET的Rebex SFTP
- edtFTPnet/PRO
或者您可以扩展SharpSsh,因为它是开源的。是的,我尝试了与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 Function
我不确定我是否理解。我可以使用Tamir.SharpSsh.jsch重命名该文件,没有问题。我只想对Tamir.SharpSsh做同样的事情。我还可以使用Bitvise Tunnelier的GUI重命名。我已经做了一百次了,现在我只想把它自动化。我会检查文件属性,但我可以发誓我已经试过了。Nickolay,SFTP确实支持文件重命名。它还支持更多。是实现(SharpSsh)不支持它们。我指的是协议规范第7页列出的SSH_FXP_重命名数据包类型:是的,协议支持此功能。但大多数SFTP协议实现都不是。是的,我尝试了与Tamir.SharpSsh.jsch类似的方法,但我觉得奇怪的是,您必须将异常设置为dwtect不存在文件。这是我不久前做的是的,这很奇怪。此外,将异常用于逻辑而不是错误处理是一种糟糕的做法。我唯一能提出的另一个建议是尝试Rebex产品(),它确实有一个
FileExists
方法来检查服务器上是否存在文件。API基本相同,因此很容易迁移到,但不是免费的。
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 Function