VB.net ftp client - how do I send?

Associate
Joined
23 Nov 2002
Posts
1,172
Location
Surrey
I'm wondering if anyone knows this. I have got the FTP example application from the Base Class Libraries code samples for VB.net, which uses the class file below to download a file from the remote ftp

What is the function to send a file to the remote ftp? Does anyone know?

Code:
Imports System.IO
Imports System.Net
Imports System.Text

Public Class SimpleFTPClient

	Public Function Download(ByVal destinationFile As String, ByVal downloadUri As Uri) As FtpStatusCode

		Try
			' Check if the URI is and FTP site
			If Not (downloadUri.Scheme = Uri.UriSchemeFtp) Then
				Throw New ArgumentException("URI is not an FTp site")
			End If

			' Set up the request
			Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(downloadUri), FtpWebRequest)

			' use the provided credentials
			If Me.m_isAnonymousUser = False Then
				ftpRequest.Credentials = New NetworkCredential(Me.m_userName, Me.m_password)
			End If

			' Download a file. Look at the other methods to see all of the potential FTP features
			ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile

			' get the response object
			Dim ftpResponse As FtpWebResponse = CType(ftpRequest.GetResponse, FtpWebResponse)
			Dim stream As Stream = Nothing
			Dim reader As StreamReader = Nothing
			Dim writer As StreamWriter = Nothing

			' get the file as a stream from the response object and write it as 
			' a file stream to the local PC
			Try
				stream = ftpResponse.GetResponseStream
				reader = New StreamReader(stream, Encoding.UTF8)
				writer = New StreamWriter(destinationFile, False)
				writer.Write(reader.ReadToEnd)
				Return ftpResponse.StatusCode
			Finally
				' Allways close all streams
				stream.Close()
				reader.Close()
				writer.Close()
			End Try
		Catch ex As Exception
			Throw ex
		End Try

	End Function

	Public Property UserName() As String
		Get
			Return Me.m_userName
		End Get
		Set(ByVal value As String)
			Me.m_userName = value
		End Set
	End Property

	Public Property Password() As String
		Get
			Return Me.m_password
		End Get
		Set(ByVal value As String)
			Me.m_password = value
		End Set
	End Property

	Public Property IsAnonymousUser() As Boolean
		Get
			Return Me.m_isAnonymousUser
		End Get
		Set(ByVal value As Boolean)
			Me.m_isAnonymousUser = value
		End Set
	End Property

	Private m_userName As String
	Private m_password As String
	Private m_isAnonymousUser As Boolean

End Class
 
There's no code in there to send a file, only receive/download one.
Obviously Public Function Download is the function for downloading... usage something along the lines of Download("C:\\blah.file", "ftp.server.com\\blah.file")
 
Hi sorry I should have made clear, I understand the code example but want to know how to expand it to be able to upload a file.
 
Back
Top Bottom