使用WinSCP .NET程序集删除目录

时间:2015-10-05 06:39:36

标签: vb.net winscp winscp-net

有问题导致找不到它:

  1. 如何检查目录是否包含任何文件夹或文件,无论是......还是空的

  2. 如何删除空目录?

  3. 即使有内容,如何删除目录。

  4. 例如,创建目录我正在使用以下功能:

    Public Sub CreateDirectory(path As String)
        If session IsNot Nothing Then
            session.CreateDirectory(path)
        End If
    End Sub
    

    将日志文件记录为requested in comment

    > 2015-10-05 11:11:13.010 MLST /\MainFolder 2014\ANIA
    < 2015-10-05 11:11:13.104 550 MLST command failed: No such file or directory.
    . 2015-10-05 11:11:13.104 Could not retrieve file information
    < 2015-10-05 11:11:13.104 Script: Can't get attributes of file '\MainFolder 2014\ANIA'.
    < 2015-10-05 11:11:13.104 Script: Could not retrieve file information
    
    < 2015-10-05 11:11:13.104 MLST command failed: No such file or directory.
    . 2015-10-05 11:11:13.104 Script: Failed
    

1 个答案:

答案 0 :(得分:1)

检查目录中是否有任何文件,使用Session.EnumerateRemoteFiles method

Dim anyFile As Boolean =
    mySession.EnumerateRemoteFiles(
        path, Nothing, EnumerationOptions.MatchDirectories).Any()

自WinSCP 5.9起支持Session.EnumerateRemoteFiles

使用旧版本的WinSCP,使用Session.ListDirectory列出目录内容并过滤掉...条目:

Dim anyFile As Boolean =
    mySession.ListDirectory(path).Files.
    Where(Function(file) (file.Name <> "..") And (file.Name <> ".")).
    Any()

要删除任何目录,无论是否为空,请使用Session.RemoveFiles

session.RemoveFiles(RemotePath.EscapeFileMask(path))