编辑:
这个问题不是重复的!,也许是一个常见的问题“这个警告是什么意思? ”但是当我问如何在特定代码中修复警告时不是重复的,而且我自己也不知道警告的意义。–
VS 中的代码分析向我显示了这个警告:
CA2202 不要多次处理对象 对象“OutputStream”可以在方法“FileSplitter.Split(String, Long, String, String, Boolean, Boolean)”中多次处理。为避免生成 System.ObjectDisposedException,您不应在对象上多次调用 Dispose。:行:490 WindowsApplication1 FileSplitter.vb 490
这条线490
是这样的:
End Using ' OutputStream
它给了我对 line 中的另一个对象同样的警告498
,这个:
End Using ' InputStream
但我认为我正确使用了Using
关键字并且我不会多次处理对象,如果我做错了什么,那么我该如何修复我的代码?
这是完整的块:
' Open the file to start reading bytes.
Using InputStream As New FileStream(fInfo.FullName, FileMode.Open)
Using BinaryReader As New BinaryReader(InputStream)
While (InputStream.Position < InputStream.Length)
' Create the chunk file to Write the bytes.
Using OutputStream As New FileStream(ChunkFile, FileMode.Create)
Using BinaryWriter As New BinaryWriter(OutputStream)
' Read until reached the end-bytes of the input file.
While (SizeWritten < ChunkSize) AndAlso (InputStream.Position < InputStream.Length)
' Some irrelevant code here...
End While ' (SizeWritten < ChunkSize) AndAlso (InputStream.Position < InputStream.Length)
OutputStream.Flush()
End Using ' BinaryWriter
End Using ' OutputStream
End While ' InputStream.Position < InputStream.Length
End Using ' BinaryReader
End Using ' InputStream
更新
添加 try/catch 块后,它仍然显示相同的警告。如果我删除最后一次尝试/捕获,它只会显示 1 个警告。
' Open the file to start reading bytes.
Dim InputStream As Stream = Nothing
Try
InputStream = New FileStream(fInfo.FullName, FileMode.Open)
Using BinaryReader As New BinaryReader(InputStream)
While (InputStream.Position < InputStream.Length)
' Create the chunk file to Write the bytes.
Dim OutputStream As Stream = Nothing
Try
OutputStream = New FileStream(ChunkFile, FileMode.Create)
Using BinaryWriter As New BinaryWriter(OutputStream)
' Read until reached the end-bytes of the input file.
While (SizeWritten < ChunkSize) AndAlso (InputStream.Position < InputStream.Length)
End Using ' BinaryWriter
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
If OutputStream IsNot Nothing Then
OutputStream.Dispose()
End If
End Try
End While ' InputStream.Position < InputStream.Length
End Using ' BinaryReader
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
If InputStream IsNot Nothing Then
InputStream.Dispose()
End If
End Try