Using recursive +.net ftpwebrequest to obtain the directory structure tree of a remote server

Source: Internet
Author: User
Tags ffi file size ftp

TreeView1 is a. Net treeview control. Please add a control to the WinForm interface

Dec in Form1_Load is an object that instantiates Ftpclient.vb, Ftpdetails is used to store FTP logon credentials.

Form1.vb

1Public Class form1class Form1


2


3 Dim ftp as FtpClient


4 Dim localfilelist as New DataTable


5


6 Private Sub Form1_Load () Sub Form1_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Myba Se. Load


7 Dim Dec as New ftpclient.ftpdetails ("www.cnblogs.com", "Cnblogvip", "**********")


8 ftp = New ftpclient (DEC)


9 Loadftptree (Treeview1.nodes, "") calls this code to mount the remote FTP structure tree


10


End Sub


12


13 ' This method is used to obtain remote directory and file tree


Private Sub loadftptree () Sub Loadftptree (ByVal Nodes as TreeNodeCollection, ByVal Path as String)


15 ' access to all levels of classification


Dim Snode as Integer =-1


17 ' Gets the current directory list via FTP


Dim result as String = Ftp.getfilelist (Path)


Dim files () as String = Split (result, vbCrLf) ' separates the text returned by the FTP server from the directory or file, file or directory name, file size


Dim ffi as New ftpclient.ftplistfileinfo


If files. Length > 0 Then


22 ' Load node


Dim I as Integer


for I = 0 to files. Length-2


FFI. Parserstring (Files (I)) ' splits the returned line. Isolate the directory or file, file or directory name, file size


If FFI. Isdirectory = True and FFI. FileName <> "..." and FFI. FileName <> "." Then ' If the directory is valid.


Snode + 1


nodes.add (0, Ffi. FileName) ' Because it is a directory, the previous arguments are saved similarly. The same function as tag, where the size of the file is saved


Dim NewPath as String = Path + "/" + FFI. FileName ' filename ' If the file represents the filename, the directory name


application.doevents ()


31 ' reads the child node of the current node load ()


Loadftptree (Nodes (Snode). Nodes, NewPath) ' recursively checks if there are subdirectories under the current directory


ElseIf FFI. isdirectory = False Then ' If it is a file


Snode + 1


Nodes.Add (FFI. Filesize.tostring, Ffi. FileName) ' Add to the node


End If


Notoginseng Next


End If


Treeview1.expandall ()


End Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.