开发者

Problems listing files with FTPWebRequest

开发者 https://www.devze.com 2023-03-14 16:20 出处:网络
I\'ve run into a couple problems uploading files to a vsftpd server using t开发者_如何转开发he .NET FTPWebRequest class.

I've run into a couple problems uploading files to a vsftpd server using t开发者_如何转开发he .NET FTPWebRequest class.

First, is there any way to list hidden files using a ListDirectoryDetails request? Right now I'm not getting any files/directories beginning with ".".

Second, when I request a listing of a directory with a name starting with "-", it returns a listing of the parent directory. For example, if I request a list of "/-DIR", I get a list of "/".

I believe these issues could be solved if a "LIST -a ./<directory>" command were sent instead of just "LIST", but trying add to the command in the .Method property of FTPWebRequest results in an exception (as per the documentation).

Is there any way to workaround this? Thanks.


I've written an FTPToolkit for an app I wrote and it lists all files and directories. Here's an example:

Public Function ListDirectoryDetail(ByVal directory As String) As FTPdirectory
        Dim ftp As System.Net.FtpWebRequest = GetRequest(GetDirectory(directory))
        'Set request to do simple list
        ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails

        Dim str As String = GetStringResponse(ftp)
        'replace CRLF to CR, remove last instance
        str = str.Replace(vbCr & vbLf, vbCr).TrimEnd(ControlChars.Cr)
        'split the string into a list
        Return New FTPdirectory(str, _lastDirectory)
End Function

Private Function GetStringResponse(ByVal ftp As FtpWebRequest) As String
        'Get the result, streaming to a string
        Dim result As String = ""
        Using response As FtpWebResponse = DirectCast(ftp.GetResponse(), FtpWebResponse)
            Dim size As Long = response.ContentLength
            Using datastream As Stream = response.GetResponseStream()
                Using sr As New StreamReader(datastream)
                    result = sr.ReadToEnd()
                    sr.Close()
                End Using

                datastream.Close()
            End Using

            response.Close()
        End Using

        Return result
End Function

If you'd like to get a copy of the Toolkit, just let me know.

0

精彩评论

暂无评论...
验证码 换一张
取 消