VB.NET • Send a File With FTP

Listing 3. You've imported the methods you want to use. All that remains is the relatively simple process of transferring the file using FTP.

Try
	' Instantiate a new FTP object
	myFtpClient = New ftp
	hInetSession = myFtpClient.Open( _
		"WebClientFtpTransfer", 0, _
		vbNullString, vbNullString, 0)
	' Check to see if Internet session was 
	' successful
	If hInetSession = 0 Then
		Throw New System.Exception( _
			"Could Not Connect to the Internet") 
	End If

' Now connect to the FTP client (include login 
' if necessary)
hFTPSession = myFtpClient.Connect(hInetSession, _
	ServerName, 21, FTPLogin, FTPPassword, 1, _
	0, 0)
' Check to see if Internet session was successful
If hFTPSession = 0 Then
	Throw New System.Exception( _
		"Could Not Connect to the FTP Server:" & _
		ServerName)
End If
' Now post the file
ftpFunctionRC = myFtpClient.PutFile(hFTPSession, _
	strFileName, strFileName, 0, 0)

' Get the results from the remote server
If ftpFunctionRC = 0 Then
	lblStatus.Text = "FTP Transfer Successful"
Else
	Throw New System.Exception(M _
		"Could Not Transfer File: " & strFileName)
End If
Catch ex As Exception
	lblStatus.Text = "ERROR UPLOADING FILE " & _
		"VIA FTP" & vbCrLf & ex.Message
	lblStatus.ForeColor = System.Drawing.Color.Red
End Try
' Close the connection
myFtpClient.CloseConnection(hInetSession)