请三个问题
1)如果下面的尝试中有例外,请求流会自动关闭,因为它在using
2)do I even need the requestStream.Close() and requestStream.Dispose() as it is in a using?
3)我是否需要关闭System.Net.FtpWebRequest?
Try
Dim rqst As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://1.com/text.txt"), System.Net.FtpWebRequest)
rqst.Credentials = New System.Net.NetworkCredential("useb", "pass")
rqst.Method = System.Net.WebRequestMethods.Ftp.UploadFile
'Throw New ApplicationException("Exception Occured")
Dim fle() As Byte = System.IO.File.ReadAllBytes("C:\test.txt")
Using requestStream As Stream = rqst.GetRequestStream()
requestStream.Write(fle, 0, fle.Length)
requestStream.Close() 'do I need this?
requestStream.Dispose() 'do I need this ?
End Using
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
答案 0 :(得分:1)
如果您使用using
,则不需要。由于using
本身用于处置。 “using”语句的原因还在于确保对象始终正确处理,并且不需要显式代码来确保发生这种情况。
当控制流退出使用块时,立即调用Dispose方法。
答案 1 :(得分:1)
是。如果在using语句中执行代码时发生异常,则流将在进入Catch块之前进行处理。