VB.NET从FTP服务器

时间:2017-02-28 02:48:37

标签: vb.net ftp timestamp filenames ftpwebrequest

我想将FTP服务器中的文件拖到本地驱动器中。但是,执行此操作最困难的部分是文件名是每天更改的时间戳。程序应该下载文件,无论日期如何变化;年,月,小时,分钟和秒。名称的格式始终相同。文件名示例以粗体显示。请指教!

例如,
用户名 - meUser
密码 - mepswrd
和FTP的URL /afea/euser/aefe/aole/efa/ 以及我希望文件在下载后保存的路径 C:\Users\alae\Desktop\loaef
并且文件是FORMAT
20160223.171234.BA_DESRP_20160121.txt

文件中唯一没有变化的部分是BA_DESRP,所有其他部分都可以更改,因为它是时间戳。

这是我的代码:

Const lf As String = "C:\Users\alae\Desktop\loaef"
Const rf As String = "/afea/euser/aefe/aole/efa/"
Const ht As String = "host"
Const un As String = "username"
Const pw As String = "password"

Dim URI As String = ht & rf
Dim ftp As System.Net.FtpWebRequest = _
    CType(FtpWebRequest.Create(URI), FtpWebRequest)

ftp.Credentials = New _
    System.Net.NetworkCredential(un, pw)

2 个答案:

答案 0 :(得分:1)

使用FtpWebRequest(或.NET框架中随时可用的任何其他功能)并不容易。你必须:

  • 使用WebRequestMethods.Ftp.ListDirectory
  • 列出远程目录
  • 将返回的列表过滤为包含BA_DESRP
  • 的列表
  • 选择最新的
  • 并下载
Dim url As String = "ftp://ftp.example.com/remote/path/"
Dim credentials As NetworkCredential = New NetworkCredential("username", "password")
Const localPath = "C:\local\path"

Dim listRequest As FtpWebRequest = WebRequest.Create(url)
listRequest.Method = WebRequestMethods.Ftp.ListDirectory
listRequest.Credentials = credentials

Dim latest As String = Nothing

Using listResponse As FtpWebResponse = listRequest.GetResponse(),
      listStream As Stream = listResponse.GetResponseStream(),
      listReader As StreamReader = New StreamReader(listStream)
    While Not listReader.EndOfStream
        Dim filename As String = listReader.ReadLine()

        If filename.Contains("BA_DESRP") Then
            Console.WriteLine("Found {0} ...", filename)

            If (latest Is Nothing) OrElse (latest < filename) Then
                latest = filename
            End If
        End If
    End While
End Using

If Not latest Is Nothing Then
    Console.WriteLine("Downloading {0} ...", latest)
    Dim webClient As New WebClient()
    webClient.Credentials = credentials
    webClient.DownloadFile(url + latest, Path.Combine(localPath, latest))
End If

或者使用具有更强大功能的其他FTP库。

例如WinSCP .NET assembly

Const localPath = "C:\local\path\"
Const remotePath = "/remote/path"

Dim sessionOptions As New SessionOptions
With sessionOptions
    .Protocol = Protocol.Ftp
    .HostName = "ftp.example"
    .UserName = "username"
    .Password = "password"
End With

Using session As New Session
    session.Open(sessionOptions)

    Dim latest As RemoteFileInfo =
        session.ListDirectory(remotePath).Files.
            Where(Function(file) file.Name.Contains("BA_DESRP")).
            OrderByDescending(Function(file) file.Name).
            FirstOrDefault()

    If Not latest Is Nothing Then
        Console.WriteLine("Downloading {0} ...", latest)
        session.GetFiles(latest.FullName, localPath).Check()
    End If
End Using

如果您可以使用实际文件时间戳(而不是其名称中的时间戳),那么它将更加直接。请参阅Downloading the most recent file

(我是WinSCP的作者)

答案 1 :(得分:0)

这将获取目录中的文件列表。由于您的文件名已经包含时间戳,您只需要从文件名中解析出来,然后最终将每个时间戳存储在一个变量中,然后您可以将下一个文件的时间戳与之比较。如果下一个文件大于变量中的值,则分配该值。最后,当你完成后,你的var应该反映你需要的文件名,在那个阶段你可以调用下载方法来获取文件。

 Dim request As FtpWebRequest = WebRequest.Create("ftp://" & "servername" & "/" & "directory" & "/*")
    request.Method = WebRequestMethods.Ftp.ListDirectory
    request.Credentials = New NetworkCredential("username", "password")

    Using reader As New StreamReader(request.GetResponse().GetResponseStream())
        Do Until reader.EndOfStream
            Console.WriteLine(reader.ReadLine())
        Loop
    End Using

因为您说数千个文件,这条路线可能不是您的最佳选择。如果您不限于.NET,您可能希望探索其他选项,例如使用ftp客户端,您可以在其中搜索文件的实际时间戳...