Public Class MemoryTransferExample
Public Sub Run(serverAddress As String, serverPort As Integer, userName As String, password As String, fileName As String)
' Create FTPConnection
Dim ftpConnection As New FTPConnection
' setting server address and credentials
ftpConnection.ServerAddress = serverAddress
ftpConnection.ServerPort = serverPort
ftpConnection.UserName = userName
ftpConnection.Password = password
' connect to server
Console.WriteLine("Connecting to server " & serverAddress)
ftpConnection.Connect()
' get the current working directory
Console.WriteLine("Working directory is " & ftpConnection.GetWorkingDirectory())
' get data to be transferred
Dim s As String = "Hello world"
Dim bytes As Byte() = Encoding.ASCII.GetBytes(s)
' upload the byte-array to a file on the server
Console.WriteLine("Uploading byte-array to the file '" & fileName & "'")
ftpConnection.UploadByteArray(bytes, fileName)
' download byte-array and display it
Console.WriteLine("Downloading the file '" & fileName & "' into a byte-array")
bytes = ftpConnection.DownloadByteArray(fileName)
Console.WriteLine("Downloaded string = " & Encoding.ASCII.GetString(bytes))
' build StringStream (defined below) for "Hello world"
Dim inStr As New StringStream(s)
' upload the stream to a file on the server
Console.WriteLine("Uploading stream to the file '" & fileName & "'")
ftpConnection.UploadStream(inStr, fileName)
inStr.Close()
' create a StringStream to download into
Dim outStr As New StringStream()
Console.WriteLine("Downloading the file '" & fileName & "' into a stream")
ftpConnection.DownloadStream(outStr, fileName)
Console.WriteLine("Downloaded string = " & outStr.ToString())
outStr.Close()
' Shut down client
Console.WriteLine("Closing client")
ftpConnection.Close()
Console.WriteLine("Example complete")
End Sub 'Run
Class StringStream
Inherits Stream
Private buffer As MemoryStream
Public Sub New()
buffer = New MemoryStream()
End Sub 'New
Public Sub New(str As String)
buffer = New MemoryStream(Encoding.ASCII.GetBytes(str))
End Sub 'New
Public Overrides Function Read(bytes() As Byte, offset As Integer, count As Integer) As Integer
Return buffer.Read(bytes, offset, count)
End Function 'Read
Public Overrides Sub Write(bytes() As Byte, offset As Integer, count As Integer)
buffer.Write(bytes, offset, count)
End Sub 'Write
Public Overrides Function Seek(offset As Long, origin As SeekOrigin) As Long
Return buffer.Seek(offset, origin)
End Function 'Seek
Public Overrides Function ToString() As String
Return Encoding.ASCII.GetString(buffer.GetBuffer())
End Function 'ToString
Public Overrides ReadOnly Property CanRead() As Boolean
Get
Return True
End Get
End Property
Public Overrides ReadOnly Property CanWrite() As Boolean
Get
Return True
End Get
End Property
Public Overrides ReadOnly Property CanSeek() As Boolean
Get
Return True
End Get
End Property
Public Overrides Sub Flush()
buffer.Flush()
End Sub 'Flush
Public Overrides ReadOnly Property Length() As Long
Get
Return buffer.Length
End Get
End Property
Public Overrides Sub SetLength(value As Long)
buffer.SetLength(value)
End Sub 'SetLength
Public Overrides Property Position() As Long
Get
Return buffer.Position
End Get
Set
buffer.Position = value
End Set
End Property
End Class 'StringStream
End Class 'MemoryTransferExample
- Hans (EDT)