How do I settle the file. Open read () correctly I am currently using the code below
using (BinaryReader br = new BinaryReader (File.OpenRead (path)) {myByte = br .ReadByte (); } While analyzing the code I get the following Visual Studio:
WARNING 1 CA2000: Microsoft. Reliability: The program 'in the program' Main (string []), 'Object' file. Open Reed (path) 'is not disposed of with all exception routes before calling' System.IDisposable.Dispose on the file. OpenRead (path) 'object before all references are doing it out of the field.
Text after "itemprop =" text ">
At first glance, it seems like a false positive, because BinaryReader will also be FileStream returned from the file. From OpenRed : From:
When the display parameter is correct, this method releases all the resources organized by any managed object that this BinaryReader Reference. This method invokes the disposal method of each referenced object.
However, there is a corner case, where FileStream is not actually dealt with: when BinaryReader throws an exception! Solution:
The correct way to write your code will be: using (var fs = File.OpenRead (path) ) {BinaryReader br = New Binary Reader (FS); MyByte = br.ReadByte (); } Background: Regarding the reference to BinaryReader only FileStream and therefore there is no need to settle it.
BTW: When using this solution for a writeable stream, it is important to flush the author before the stream is dealt:
Usage (var file stream = new file stream (...)) {var author = new streamer (file stream); Writer.WriteLine (...); Writer.flush (); // & lt; - Important} If you forget it, then everything that is written in your stream by using StreamWriter can not be done.
Comments
Post a Comment