. Net does not have a dedicated FTP class. We can call the ftp. EXE that comes with the system or the wininet. dll in Win32 API to complete basic operations. I hope the following code can help you.
Method 1: Use ftp.exe to call it through the process class.
Imports system. Diagnostics
...
Public sub getfilebycallftp ()
'Define the start information of processstartinfo and process.
Dim PSI as new processstartinfo
The path of 'ftp.exe is best placed in the configuration file.
PSI. filename = "C:/winnt/system32/ftp.exe"
PSI. redirectstandardinput = false
PSI. redirectstandardoutput = true
'Indicates that the process is not started using the operating system shell program.
PSI. useshellexecute = false
'COMMAND set file name. Note that the path cannot contain spaces.
Dim filename as string = "C/ftp.txt"
'-S: Filename indicates that the control command is read from the file.
PSI. Arguments = "-S:" + filename
Dim proc as process
Proc = process. Start (PSI)
'Wait for the process to complete the task
Proc. waitforexit ()
'Output the result on the console
Console. writeline (Proc. standardoutput)
Console. Readline ()
End sub
========================================================== ==============================================
Content in ftp.txt
Method 2: Use Win32 API -- wininet. dll
First, the API declaration:
This test program is VB. NET consoleapplication. Therefore, the API declaration is written in the module,
The method is static. Therefore, the shared keyword is not added. Please note this.
<Dllimport ("wininet")> _
Public Function internetopen (byval sagent as string, byval laccesstype as integer, byval sproxyname as string ,_
Byval sproxybypass as string, byval lflags as integer) as integer
End Function
<Dllimport ("wininet")> _
Public Function internetconnect (byval hinternetsession as integer, byval sservername as string ,_
Byval nserverport as integer, byval susername as string ,_
Byval spassword as string, byval lservice as integer ,_
Byval lflags as integer, byval lcontext as integer) as integer
End Function
<Dllimport ("wininet")> _
Public Function ftpgetfile (byval hftpsession as integer, byval lpszremotefile as string ,_
Byval lpsznewfile as string, byval ffailifexists as Boolean ,_
Byval dwflagsandattributes as integer, byval dwflags as integer ,_
Byval dwcontext as integer) as Boolean
End Function
<Dllimport ("wininet")> _
Public Function internetclosehandle (byval hinet as integer) as integer
End Function
Call:
Public sub getfilebycallwininetdll ()
Try
Dim intinet as integer = internetopen (nothing, 0, nothing, nothing, 0)
If intinet> 0 then
'Parameter: intinet session value, FTP address, port, user name, password, lservice, lflags, lcontext
Dim intinetconn as integer = internetconnect (intinet, "192.168.110.152", 0, "tokiwa", "tokiwa", 1, 0, 0)
If intinetconn> 0 then
'Download an object to a specified object
Dim RET as Boolean = ftpgetfile (intinetconn, "pagerror.gif", "C:/itest.gif", 0, 0, 1, 0)
If RET then
Console. writeline ("OK! ")
Console. Readline ()
End if
Internetclosehandle (intinetconn)
Internetclosehandle (intinet)
Else
Console. writeline ("can't connect! ")
Console. Readline ()
End if
Else
Console. writeline ("FTP wrong! ")
Console. Readline ()
End if
Catch ex as exception
Console. writeline (ex. Message)
Console. Readline ()
End try
End sub