代码之家  ›  专栏  ›  技术社区  ›  sinDizzy

sharpssh进度和重命名

  •  0
  • sinDizzy  · 技术社区  · 14 年前

    使用库连接到远程服务器并复制文件。我的流程运行得相当好,但是有一些小的问题我似乎无法解决,因为库的文档相当薄。

    我有两个日常工作。一个使用tamir.sharpssh类,另一个使用tamir.sharpssh.jsch类。

    1. 使用tamir.sharpssh类,我可以将文件从本地服务器复制到远程服务器,并点击Pogres事件。我不能做的是确定远程服务器上的特定文件say/report/data.txt是否存在于服务器上。如果它存在或不存在,我需要采取不同的行动。另外,如何重命名远程服务器上的文件。我尝试使用带有“rename”、“rn”和“mv”命令的sshexec,但似乎不起作用。

    2. 使用tamir.sharpssh.jsch,我可以将文件从本地服务器复制到远程服务器。我还可以重命名远程服务器上的文件。我对这个类不能做的是利用进度事件来跟踪复制进度。另外,我似乎找不到一种好方法来测试服务器上是否存在特定的文件。我想出来的是粗糙的,我唯一能想到的测试方法就是使用

          Dim c As ChannelSftp
          Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile)
          Dim cnt As Integer = vct.Count
      

    当一个或多个文件存在时,我得到一个计数,没问题。如果没有文件,则引发异常。

    不管怎样,我的日常工作只是一些小事情,我需要帮助。

    蒂亚 自动增益控制

    3 回复  |  直到 13 年前
        1
  •  2
  •   Nobody    14 年前

    你可以打电话给 Tamir.SharpSsh.Sftp.GetFile 方法使用要检查的文件的路径exists(例如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 方法或递归获取和放置。但总的来说,这项工作已经完成了。

    简单的事实是,tamir.sharpssh不能远程重命名文件-它只是不实现该功能。您可以购买具有更多功能的更好的库,例如:

    • Kellerman软件.NET SFTP库
    • WODSFPT.NET
    • 用于.NET的Rebex SFTP
    • EDTFPTNET/PRO

    或者您可以扩展sharpssh,因为它是开源的。

        2
  •  0
  •   Nickolay Olshevsky    14 年前

    您的问题是因为SFTP协议的限制。 -要检查文件是否存在,请尝试返回该文件的属性; -大多数服务器目前不支持文件重命名。

        3
  •  0
  •   sinDizzy    14 年前

    是的,我尝试了类似于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