Public Shared Function RestartDownloadFromServer(ByVal fileName As String, ByVal serverUri As Uri, ByVal offset As Long) As Boolean
' The serverUri parameter should use the ftp:// scheme.
' It identifies the server file that is to be downloaded
' Example: ftp://contoso.com/someFile.txt.
' The fileName parameter identifies the local file.
'The serverUri parameter identifies the remote file.
' The offset parameter specifies where in the server file to start reading data.
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.DownloadFile
request.ContentOffset = offset
Dim response As FtpWebResponse = Nothing
Try
response = CType(request.GetResponse(), FtpWebResponse)
Catch e As WebException
Console.WriteLine(e.Status)
Console.WriteLine(e.Message)
Return False
End Try
' Get the data stream from the response.
Dim newFile As Stream = response.GetResponseStream()
' Use a StreamReader to simplify reading the response data.
Dim reader As New StreamReader(newFile)
Dim newFileData As String = reader.ReadToEnd()
' Append the response data to the local file
' using a StreamWriter.
Dim writer As StreamWriter = File.AppendText(fileName)
writer.Write(newFileData)
' Display the status description.
' Cleanup.
writer.Close()
reader.Close()
response.Close()
Console.WriteLine("Download restart - status: {0}", response.StatusDescription)
Return True
End Function