因爲我在這裏使用「使用」,如果在TRY中的任何地方有任何異常,FtpWebRequest,FtpWebRespons和responseStream會自動關閉?如果在「使用」中出現異常,它會自動關閉
Try
Dim request As FtpWebRequest = CType(WebRequest.Create(""), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails
request.Credentials = New NetworkCredential("", "")
Using response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
Using responseStream As Stream = response.GetResponseStream()
Using reader As New StreamReader(responseStream)
TextBox1.Text = reader.ReadToEnd
TextBox1.Text += vbNewLine
TextBox1.Text += vbNewLine
' Use the + for appending (set the textbox to multiline)
End Using
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
爲了測試你自己,創建一個實現'IDisposable'的類,並在'Dispose'方法中寫入一條消息給控制檯(或者放一個斷點) –