【问题标题】:FtpWebRequest: create nested directories (local vs remote)FtpWebRequest:创建嵌套目录(本地与远程)
【发布时间】:2016-05-07 12:23:49
【问题描述】:

目标:

我想在上传文件之前确保 FTP 路径存在,如果不存在 ==> 创建它。

我正在使用的代码:

Dim ftpPath As String = "ftp://----------/ParentDir/SubFolder1/SubFolder2"
If Not FTPDirExists(ftpPath) Then
    CreateFTPDir(ftpPath)
End If

CreateFTPDir 在哪里:

Private Sub CreateFTPDir(DirPath As String)
    Dim request As FtpWebRequest = FtpWebRequest.Create(DirPath)
    request.Credentials = New NetworkCredential("UserName", "Password")
    request.Method = WebRequestMethods.Ftp.MakeDirectory
    request.Proxy = Nothing
    request.KeepAlive = True
    Try
        Dim resp As FtpWebResponse = request.GetResponse()
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try
End Sub

现在,当我在本地 FTP 服务器(使用 FileZilla 创建)上测试此代码时,无论嵌套目录的数量如何,它都会创建路径。但是当我在实际 ( remote) FTP 服务器,它会抛出以下异常:The remote server returned an error: (550) File unavailable 如果要创建的目录不止一个。

我的问题是为什么本地服务器不会出现这个问题?我必须在远程服务器上分别创建每个 nested 目录吗?


附加信息 + 第二个问题:

这是我正在使用的FTPDirExists 函数(经过大量搜索后我能想到的最好的):

Private Function FTPDirExists(DirPath As String) As Boolean
    DirPath &= If(DirPath.EndsWith("/"), "", "/")
    Dim request As FtpWebRequest = FtpWebRequest.Create(DirPath)
    request.Credentials = New NetworkCredential("UserName", "Password")
    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails
    request.Proxy = Nothing
    request.KeepAlive = True
    Try
        Using resp As FtpWebResponse = request.GetResponse()
            Return True
        End Using
    Catch ex As WebException
        Dim resp As FtpWebResponse = DirectCast(ex.Response, FtpWebResponse)
        If resp.StatusCode = FtpStatusCode.ActionNotTakenFileUnavailable Then
            Return False ' ==> Unfortunately will return false for other reasons (like no permission).
        Else
            Return False ' ==> Don't bother about this.
        End If
    End Try
End Function

正如我在上面的评论中提到的那样,它不是 100% 准确的,所以如果您有更准确的方法,请告诉我。

【问题讨论】:

    标签: .net vb.net ftpwebrequest


    【解决方案1】:

    我决定使用另一个函数来分别创建路径的每个文件夹:

    Public Shared Sub CreatePath(RootPath As String, PathToCreate As String, Cred As NetworkCredential)
        Dim request As FtpWebRequest
        Dim subDirs As String() = PathToCreate.Split("/"c)
        Dim currentDir As String = If(RootPath.EndsWith("/"), RootPath.Substring(0, RootPath.Length - 1), RootPath)
        For Each subDir As String In subDirs
            currentDir &= "/" & subDir
    
            request = DirectCast(FtpWebRequest.Create(currentDir), FtpWebRequest)
            request.Credentials = Cred
            request.Method = WebRequestMethods.Ftp.MakeDirectory
            request.Proxy = Nothing
            Try
                Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
                response.Close()
            Catch ex As Exception
    
            End Try
        Next
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2012-02-19
      • 2015-05-13
      相关资源
      最近更新 更多