简体   繁体   English

使用FTPWebRequest列出文件时出现问题

[英]Problems listing files with FTPWebRequest

I've run into a couple problems uploading files to a vsftpd server using the .NET FTPWebRequest class. 使用.NET FTPWebRequest类将文件上传到vsftpd服务器时遇到了几个问题。

First, is there any way to list hidden files using a ListDirectoryDetails request? 首先,有什么方法可以使用ListDirectoryDe​​tails请求列出隐藏文件? 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 "/". 例如,如果我请求“ / -DIR”列表,则会得到“ /”列表。

I believe these issues could be solved if a "LIST -a ./&ltdirectory>" 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). 我相信,如果发送的是“ LIST -a ./& ltdirectory>”命令而不是“ LIST”,则可以解决这些问题,但是尝试将FTPWebRequest的.Method属性添加到命令中会导致异常(根据文档)。

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. 我已经为编写的应用程序编写了FTPToolkit,它列出了所有文件和目录。 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. 如果您想获得该工具包的副本,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM