C#C
C#9mo ago
zed

✅ StreamReader returns empty string on valid file stream

I have this code:
            using var reader = new StreamReader(stream, Encoding.UTF8, false, 0x400, true);
            str = reader.ReadToEnd();

After this, str is empty. The stream, however, is valid. The Length property matches the length of the file, it was opened with the Read permission and the CanRead property is true. The position was at 0, and has not been advanced and is still at 0 after the read. The attached screenshot shows the stream (and str) variables after the ReadToEnd call.

To make sure the stream is valid, I added this after the ReadToEnd:
        using var temp = File.Open(@"C:\log\bla", FileMode.Create, FileAccess.Write, FileShare.None);
        stream.CopyTo(temp);

And this correctly writes the full file, in all its glory, to the provided location, which should confirm that the issue is not with the stream itself, and all file permissions of the stream have been set correctly.

I'm using .NET Framework 4.8.

What could be the issue here?
image.png
Was this page helpful?