在VB.NET中从FTP文件夹下载所有文件和子目录 [英] Download all files and sub-directories from FTP folder in VB.NET

查看:633
本文介绍了在VB.NET中从FTP文件夹下载所有文件和子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上搜索了关于我的问题的所有内容,但是我只找到了C#的答案,但没有找到适用于VB.NET的答案。



我想要下载所有文件并我的FTP服务器上一个目录的子目录。



我正在从我的FTP服务器上下载一个ZIP文件并解压缩它,但这不是一个好方法。

谢谢!

解决方案

I知道你可能会懒惰地阅读所有这些,但这是答案!代码到底:D



你好,



使用可用于C#和VB.NET的WinSCP库非常简单 - 下面是完整的答案:

首先,将WinSCP库安装到您的项目中,请参阅这里: https://winscp.net/eng/docs/library#downloading_and_installing_the_assembly



或者如果您懒得去做,那么只需下载该库并在项目中添加该DLL文件作为参考。



现在让我们将整个目录(包括其文件及其子目录)下载到本地存储中。



您有2个选项:




  • Session.GetFiles 即使文件&目录已经下载。

  • Session .SynchronizeDirectories 来下载文件&如果它们不存在,则会下载已存在的已修改文件。 (BEST和优于第一种选择)。 永远。然而,如果你想使用Session.GetFiles方法...你可以在这里看到VB.NET的例子: https://winscp.net/eng/docs/library_session_getfiles#vbnet



    以下是我如何使用第二个选项: https://winscp.net/eng/docs/faq_script_modified_files



    基本上使用同步本地 c $ c>而不是 get 同步远程
    而不是 put



    使用WinSCP .NET程序集时,请使用
    Session.SynchronizeDirectories,参数模式设置为
    SynchronizationMode。 Remote或SynchronizationMode.Local分别代替
    Session.GetFiles或Session.PutFiles。


    因为我想从FTP服务器下载文件到我的本地PC,那么这里是我使用的(SynchronizeDirectories):


    1. 所以是的!当然 Imports WinSCP first: - ) 使用这段代码,因为 https://winscp.net/eng/docs/library_session_synchronizedirectories#vbnet 适用于SFTP(端口22)而非FTP (端口21)......并且它使用从PC上传到FTP服务器的 SynchronizeMode.Remote ,所以在下面的代码中,我将 。远程 .Local

       公开共享函数Main()As Integer 

      尝试
      '安装会话选项
      Dim sessionOptions As New SessionOptions
      with sessionOptions
      .Protocol = Protocol.Ftp
      .HostName =example.com
      .UserName =user
      .Password =mypassword
      End With

      使用会话为新会话
      '会不断重新端口进度同步
      AddHandler session.FileTransferred,AddressOf FileTransferred

      '连接
      session.Open(sessionOptions)

      '同步文件
      Dim synchronizationResult As SynchronizationResult
      synchronizationResult = _
      session.SynchronizeDirectories(_
      SynchronizationMode.Local,d:\www,/ home / martin / public_html,False)

      '抛出任何错误
      synchronizationResult.Check()
      结束使用

      返回0
      捕获e作为异常
      Console.WriteLine (Error:{0},e)
      返回1
      结束尝试

      结束函数

      Private Shared Sub FileTransferred(ByVal sender As Object ,ByVal e As TransferEventArgs)

      如果e.Error是Nothing然后
      'Console.WriteLine({0}上载成功,e.File名称)
      另外
      'Console.WriteLine({0}上传失败:{1},e.FileName,e.Error)
      End If

      如果e.Chmod IsNot Nothing Then
      如果e.Chmod.Error是Nothing Then
      'Console.WriteLine({0}的权限设置为{1},e.Chmod.FileName,e .Chmod.FilePermissions)
      Else
      'Console.WriteLine(设置{0}的权限失败:{1},e.Chmod.FileName,e.Chmod.Error)
      End如果
      否则
      'Console.WriteLine({0}的权限保留其默认值,e.Destination)
      End If

      如果e.Touch IsNot Nothing Then
      如果e.Touch.Error是Nothing然后
      'Console.WriteLine({0}的时间戳设置为{1},e.Touch.FileName,e.Touch.LastWriteTime)
      '
      'Console.WriteLine(设置{0}的时间戳失败:{1},e.Touch.FileName,e.Touch.Error)
      End If
      Else
      '这绝不应该发生在本地到远程同步期间
      'Console.WriteLine({0}的时间戳保留其默认值(当前时间),e.Destination)
      结束如果

      结束Sub


    不要忘记替换凭证和路径。

    还有一件事?祝你的项目好运! : - )

    I searched all over the internet about my question but I found answer for C# only, but none for VB.NET.

    I want to download all the files and sub-directories of a directory on my FTP server.

    I am currently doing it by downloading a ZIP file from my FTP server and extracting it but this is not a good method.

    Thanks!

    解决方案

    I KNOW YOU MIGHT BE LAZY TO READ ALL THAT, BUT THIS IS THE ANSWER!!! THE CODE IS AT THE END :D

    Hello,

    It is very simple to do using the WinSCP library which is available for C# and VB.NET - here is the full answer:

    Firsly, install the WinSCP library to your project, see here: https://winscp.net/eng/docs/library#downloading_and_installing_the_assembly

    Or if you are lazy to do it, then just download the library and add the DLL file as a reference in your project.

    And now let's download the whole directory including its files and its sub-directories to the local storage.

    You have 2 options:

    • Session.GetFiles to download even if the files & directories are already downloaded.

    • Session.SynchronizeDirectories to download the files & directories if they does not exists, and also will download the the modified files which already exists. (BEST and better than the first option).

    I used the second option of course because it is the best one ever. However, if you want to use the Session.GetFiles method... you can see the VB.NET example here: https://winscp.net/eng/docs/library_session_getfiles#vbnet

    And here is how I used the second option: https://winscp.net/eng/docs/faq_script_modified_files

    As you can see, everything is explained great!

    Basically use synchronize local instead of get and synchronize remote instead of put.

    With WinSCP .NET assembly that means, use Session.SynchronizeDirectories, with argument mode set to SynchronizationMode.Remote or SynchronizationMode.Local instead of Session.GetFiles or Session.PutFiles respectively.

    And as I wanted to download the files from the FTP Server to my local PC, then here is what I used (SynchronizeDirectories):

    1. So yes! Of course Imports WinSCP first :-)
    2. Use this code, because the code on https://winscp.net/eng/docs/library_session_synchronizedirectories#vbnet is for SFTP (port 22) and not FTP (port 21) ... and also it uses SynchronizeMode.Remote which uploads from your PC to the FTP server , so in the code below I replaced .Remote with .Local

      Public Shared Function Main() As Integer
      
          Try 
              ' Setup session options
              Dim sessionOptions As New SessionOptions
              With sessionOptions
                  .Protocol = Protocol.Ftp
                  .HostName = "example.com"
                  .UserName = "user"
                  .Password = "mypassword"
              End With
      
              Using session As New Session
                  ' Will continuously report progress of synchronization
                  AddHandler session.FileTransferred, AddressOf FileTransferred
      
                  ' Connect
                  session.Open(sessionOptions)
      
                  ' Synchronize files
                  Dim synchronizationResult As SynchronizationResult
                  synchronizationResult = _
                      session.SynchronizeDirectories( _
                          SynchronizationMode.Local, "d:\www", "/home/martin/public_html", False)
      
                  ' Throw on any error
                  synchronizationResult.Check()
              End Using
      
              Return 0
          Catch e As Exception
              Console.WriteLine("Error: {0}", e)
              Return 1
          End Try
      
      End Function
      
      Private Shared Sub FileTransferred(ByVal sender As Object, ByVal e As TransferEventArgs)
      
          If e.Error Is Nothing Then
              'Console.WriteLine("Upload of {0} succeeded", e.FileName)
          Else
              'Console.WriteLine("Upload of {0} failed: {1}", e.FileName, e.Error)
          End If
      
          If e.Chmod IsNot Nothing Then
              If e.Chmod.Error Is Nothing Then
                  'Console.WriteLine("Permisions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions)
              Else
                  'Console.WriteLine("Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error)
              End If
          Else
              'Console.WriteLine("Permissions of {0} kept with their defaults", e.Destination)
          End If
      
          If e.Touch IsNot Nothing Then
              If e.Touch.Error Is Nothing Then
                  'Console.WriteLine("Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime)
              Else
                  'Console.WriteLine("Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error)
              End If
          Else
              ' This should never happen during "local to remote" synchronization
              'Console.WriteLine("Timestamp of {0} kept with its default (current time)", e.Destination)
          End If
      
      End Sub
      

    Don't forget to replace the credentials and the paths..

    One more thing? Good luck on your project! :-)

    这篇关于在VB.NET中从FTP文件夹下载所有文件和子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆