Download
. NET does not specifically handle FTP classes, we can do the basic operation by invoking the FTP.EXE of the system itself or by invoking the Wininet.dll in the Win32 API. Hope that the following code for everyone.
Method One: Use Ftp.exe to invoke it through the process class.
Imports System.Diagnostics
...
Public Sub getfilebycallftp ()
' Defines the startup information for the processstartinfo,process.
Dim psi as New ProcessStartInfo
The ' Ftp.exe ' path is best placed in the configuration file.
Psi. FileName = "C:\WINNT\system32\ftp.exe"
Psi. Redirectstandardinput = False
Psi. Redirectstandardoutput = True
' This value indicates that the process is not started using the operating system shell program.
Psi. UseShellExecute = False
' Command set file name. Note that there must be no spaces in the path.
Dim fileName as String = "C\ftp.txt"
'-s:filename, read the control command 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 results on console
Console.WriteLine (Proc. StandardOutput)
Console.ReadLine ()
End Sub
==============================================================================
What's in Ftp.txt.
Method two, using Win32 Api--wininet.dll
The first is that the API declares:
Because this test program is vb.net consoleapplication so, the API declaration is written in module,
The method is static. So don't add the shared keyword, so please be aware of 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
' Parameters: Intinet Session value, FTP address, port, username, 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 a file to the specified file
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