Public Shared Function AppendFileOnServer(ByVal fileName As String, ByVal serverUri As Uri) As Boolean
' The URI described by serverUri should use the ftp:// scheme.
' It contains the name of the file on the server.
' Example: ftp://contoso.com/someFile.txt.
' The fileName parameter identifies the file containing
' the data to be appended to the file on the server.
If serverUri.Scheme <> Uri.UriSchemeFtp Then
Return False
End If
' Get the object used to communicate with the server.
Dim request As FtpWebRequest = CType(WebRequest.Create(serverUri), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.AppendFile
Dim sourceStream As New StreamReader(fileName)
Dim fileContents() As Byte = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())
sourceStream.Close()
request.ContentLength = fileContents.Length
' This example assumes the FTP site uses anonymous logon.
request.Credentials = New NetworkCredential("anonymous", "janeDoe@contoso.com")
Dim requestStream As Stream = request.GetRequestStream()
requestStream.Write(fileContents, 0, fileContents.Length)
requestStream.Close()
Dim response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
Console.WriteLine("Append status: {0}", response.StatusDescription)
response.Close()
Return True
End Function