Difference between StreamReader.Read and StreamReader.ReadBlock

ReadBlock does not mean it is thread safe. If you use Reflector to look at the implementation of StreamReader.ReadBlock (which is inherited from TextReader.ReadBlock), all it does is make multiple calls to the “Read” method until either the “Read” method returns 0 or we have read as many bytes as requested. This is needed because … Read more

Will a using clause close this stream?

Yes, StreamReader.Dispose closes the underlying stream (for all public ways of creating one). However, there’s a nicer alternative: using (TextReader reader = File.OpenText(“file.txt”)) { } This has the added benefit that it opens the underlying stream with a hint to Windows that you’ll be accessing it sequentially. Here’s a test app which shows the first … Read more

Do I need to explicitly close the StreamReader in C# when using it to load a file into a string variable?

No, this will not close the StreamReader. You need to close it. Using does this for you (and disposes it so it’s GC’d sooner): using (StreamReader r = new StreamReader(“file.txt”)) { allFileText = r.ReadToEnd(); } Or alternatively in .Net 2 you can use the new File. static members, then you don’t need to close anything: … Read more

Memory Leak using StreamReader and XmlSerializer

The leak is here: new XmlSerializer(typeof(XMLObj), new XmlRootAttribute(“rootNode”)) XmlSerializer uses assembly generation, and assemblies cannot be collected. It does some automatic cache/reuse for the simplest constructor scenarios (new XmlSerializer(Type), etc), but not for this scenario. Consequently, you should cache it manually: static readonly XmlSerializer mySerializer = new XmlSerializer(typeof(XMLObj), new XmlRootAttribute(“rootNode”)) and use the cached serializer … Read more

How do I open an already opened file with a .net StreamReader?

As Jared says, You cannot do this unless the other entity which has the file open allows for shared reads. Excel allows shared reads, even for files it has open for writing. Therefore, you must open the filestream with the FileShare.ReadWrite parameter. The FileShare param is often misunderstood. It indicates what other openers of the … Read more

An elegant way to consume (all bytes of a) BinaryReader?

Original Answer (Read Update Below!) Simply do: byte[] allData = read1.ReadBytes(int.MaxValue); The documentation says that it will read all bytes until the end of the stream is reached. Update Although this seems elegant, and the documentation seems to indicate that this would work, the actual implementation (checked in .NET 2, 3.5, and 4) allocates a … Read more

tech