Im trying to do a FTPWEBREQUEST in a timer but I get this error on TextBox1.Text = reader.ReadToEnd
Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
can you help?
thanks
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
Dim request As FtpWebRequest = CType(WebRequest.Create(""), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails
request.Credentials = New NetworkCredential("", "")
Dim response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
Dim ResponseStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(ResponseStream)
'Console.Write(reader.ReadToEnd)
'MessageBox.Show("Directory List Complete, status {0}", response.StatusDescription)
'MessageBox.Show(reader.ReadToEnd)
'MessageBox.Show(reader.ReadToEnd)
TextBox1.Text = reader.ReadToEnd
TextBox1.Text = vbNewLine
TextBox1.Text = vbNewLine
ResponseStream.Close()
reader.Close()
response.Close()
reader.Close()
response.Close()
Timer1.Start()
End Sub
End Class
I don’t know your goal exactly and why you need a timer.
But I want to give you some advices for you coding practice:
You should not close (dispose) your streams twice. Use the
Usingstatement for more clean andreadable code. It also helps to release the resources early, even a exception is thrown.