vb.net 如何列出 FTP 连接的目录内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14076973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list directory contents of an FTP connection
提问by Dave Fes
I can't find a tutorial on this. In VB.NET I want to do a command such as:
我找不到这方面的教程。在 VB.NET 中,我想做一个命令,例如:
Dim array1() as string = ListFilesInFolder("www.example.com/images")
I know this is probably not going to be that simple, but can anyone point me to a tutorial or anything?
我知道这可能不会那么简单,但是谁能给我指点教程或其他任何东西?
回答by Steve
use this free library http://netftp.codeplex.com/
使用这个免费库http://netftp.codeplex.com/
Imports System.Net
Imports System.Net.FtpClient
Sub Main
using ftp = new FtpClient()
ftp.Host = "www.example.com"
ftp.Credentials = new NetworkCredential("yourFTPUser", "yourFTPPassword")
ftp.SetWorkingDirectory("/images")
for each item in ftp.GetListing(ftp.GetWorkingDirectory())
select case item.Type
case FtpFileSystemObjectType.Directory:
Console.WriteLine("Folder:" + item.FullName)
case FtpFileSystemObjectType.File:
Console.WriteLine("File:" + item.FullName)
End Select
Next
End Using
End Sub
of course I'm assuming that www.example.com is a FTP server.
当然,我假设 www.example.com 是一个 FTP 服务器。
AN IMPORTANT NOTE:The library requires the complete Framework 4.0
. You should go to the Build Page of your Project Properties
, click on the Advanced Options
and select Framework 4.0
instead of Framework 4.0 Client Profile
重要提示:该库需要完整的Framework 4.0
. 您应该转到 的构建页面Project Properties
,单击Advanced Options
并选择Framework 4.0
而不是Framework 4.0 Client Profile
回答by Vivek S.
The following method will work for framework 3.5
and higher, I know this question is 3 years old but I ran into a situation where I need to list FTP directories in a framework 3.5
project So I wrote following code by referring How to: List Directory Contents with FTP.
以下方法适用于framework 3.5
及更高版本,我知道这个问题已有3 年历史,但我遇到了需要在framework 3.5
项目中列出 FTP 目录的情况,因此我参考How to: List Directory Contents with FTP编写了以下代码。
Imports System.Net
Dim Dirlist As New List(Of String) 'I prefer List() instead of an array
Dim request As FtpWebRequest = DirectCast(WebRequest.Create("ftp://www.example.com/images"), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.ListDirectory
request.Credentials = New NetworkCredential("USER_NAME", "PASSWORD")
Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
Dim responseStream As Stream = response.GetResponseStream
Using reader As New StreamReader(responseStream)
Do While reader.Peek <> -1
Dirlist.Add(reader.ReadLine)
Loop
End Using
response.Close()