This is a follow-up to a question that was answered very nicely with a C# example, but I'm looking for a VB.NET (2.0 or 3.5) example.
How does one get and put files with FTP using Visual Basic .NET (and Visual Studio 2008)?
I'm working on a ftp control and here is a test class I started with.
Imports System.Net
Imports System.IO
Public Class GetFile
Friend Sub subGetFile(ByVal uri As String, ByVal LocalFile As String)
My.Computer.Network.DownloadFile(uri, LocalFile, My.Settings.strUser, My.Settings.strPwd, True, 100000, True)
End Sub
Friend Sub subDownLoadFile(ByVal strUri As String, ByVal strLocalFile As String)
Dim Instance As New Uri(strUri)
Dim Request As FtpWebRequest
Request = CType(WebRequest.Create(Instance), FtpWebRequest)
With Request
.Method = "RETR"
.UseBinary = False
.Timeout = 120000
End With
Dim Response As FtpWebResponse = CType(Request.GetResponse(), FtpWebResponse)
Dim ResponseStream As Stream
Dim TargetStream As Stream
Try
Response = CType(Request.GetResponse(), FtpWebResponse)
TargetStream = New FileStream(strLocalFile, FileMode.Create)
Using Writer As New StreamWriter(TargetStream)
ResponseStream = Response.GetResponseStream()
Using Reader As New StreamReader(ResponseStream, System.Text.Encoding.UTF8)
Writer.Write(Reader.ReadToEnd())
Reader.Close()
End Using
Writer.Close()
End Using
Catch Err As Exception
MessageBox.Show(Err.Message)
Finally
Response.Close()
End Try
End Sub
End Class
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment