C
C#β€’4d 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();
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);
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?
No description
28 Replies
zed
zedOPβ€’4d ago
Oh, shit, almost forgot to mention the funnest part It works sometimes on the exact same file x.x But the screenshot is still from a failed attempt (I added a breakpoint with the str == "" condition) So even in the cases where it fails, it has a valid stream, that can successfully write to a file, but the StreamReader still fails to use it correctly...
canton7
canton7β€’4d ago
... Huh. What does File.ReadAllText do? (That should do exactly the same internally, which means it's a good way to figure out whether the problem is with the file itself (or maybe the SR), or whether it's something to do with your FileStream)
ero
eroβ€’4d ago
the entire code snippet might be good to look over just for sanity i guess
zed
zedOPβ€’4d ago
Hm, will need to see how to best check that πŸ˜… At the point I'm at, I only have the stream available, but give me a few I'll see if I can trim it down properly, this code is a bit involved and the stream is created in a base class, then used several classes down in the inheritance hierarchy
ero
eroβ€’4d ago
owch
zed
zedOPβ€’4d ago
Ugh, I think I have it, though it's hard to reliably reproduce it... @canton7 I think that File.ReadAllText throws an exception, where reader.ReadToEnd just returns an empty string... and CopyTo also does not seem to complain, despite not writing anything. I stated in the opening post, that the file gets successfully written, but that was a derp on my part <_< The file reading process was just started multiple times, and the second or third or w/e time it worked, and overwrite the 0B file the failed attempt created with the proper file... I'm still not sure why neither File.Open nor reader.ReadToEnd nor stream.CopyTo throw an exception though :\ File.ReadAllText did throw...
canton7
canton7β€’4d ago
Read the source for ReadAllText, it literally just does what you're doing
zed
zedOPβ€’4d ago
Unfortunately I can't guarantee that the case where File.ReadAllText throws is the same as where reader.ReadToEnd fails...
canton7
canton7β€’4d ago
What's the exception?
zed
zedOPβ€’4d ago
IOException, I can get you the error message in a sec One difference is that it uses a different Stream constructor
canton7
canton7β€’4d ago
Yeah, which might help narrow down the cause
zed
zedOPβ€’4d ago
System.IO.IOException: 'The process cannot access the file 'C:\Code\...' because it is being used by another process.'
System.IO.IOException: 'The process cannot access the file 'C:\Code\...' because it is being used by another process.'
I'll see if modifying the Stream constructor would help get that exception later on
canton7
canton7β€’4d ago
Hmm, evidence is starting to point in the same direction here
zed
zedOPβ€’4d ago
Ok, I figured it out, and in retrospect, it's kinda obvious... though I still dislike that behavior by the reader I opened the stream with ReadWrite sharing permissions And another program was writing Which the stream accepted And the reader then just produced an empty output Still not sure how I feel about that behavior by the reader πŸ˜… But explains it, and easy to change Read is the correct share permission here anyway, no idea why it was ReadWrite Thanks for the help, as usual 🫑
canton7
canton7β€’4d ago
Glad you got to the bottom of it! I wouldn't have expected that behaviour either, but then I haven't played with ReadWrite much
zed
zedOPβ€’4d ago
Same, but good to know
Optimum
Optimumβ€’4d ago
you need to close ur stream if
using var reader = new StreamReader(stream, Encoding.UTF8, false, 0x400, true);
str = reader.ReadToEnd();
using var reader = new StreamReader(stream, Encoding.UTF8, false, 0x400, true);
str = reader.ReadToEnd();
exists in the same place as
using var temp = File.Open(@"C:\log\bla", FileMode.Create, FileAccess.Write, FileShare.None);
stream.CopyTo(temp);
using var temp = File.Open(@"C:\log\bla", FileMode.Create, FileAccess.Write, FileShare.None);
stream.CopyTo(temp);
then u need to close that stream properly
canton7
canton7β€’4d ago
Err that's obviously not the issue
Optimum
Optimumβ€’4d ago
using var just closes the stream IIRC at the END of the whole method
The process cannot access the file 'C:\Code...' because it is being used by another process.
canton7
canton7β€’4d ago
Read the backlog
zed
zedOPβ€’4d ago
The stream needs to remain open in my case, it's being closed by our file reading harness outside of that method But my issue was why it didn't throw an exception, along with some observational failures on my part But it's all good now πŸ‘
Optimum
Optimumβ€’4d ago
why? why must it remain open? just curious
zed
zedOPβ€’4d ago
Because it's being closed later
Optimum
Optimumβ€’4d ago
Why can't u read, offload the bytes and close
zed
zedOPβ€’4d ago
This is a complicated file reading harness that handles a bunch of issues The inner method (which is where the above code happened) does not own the stream It only accesses it partially
Optimum
Optimumβ€’4d ago
Read only stream πŸ˜„ It's solved now
cned
cnedβ€’4d ago
My guess is that the stream is at the end of data already. Are you sure that whatever the source of the stream is has the Position at the front? If not, ReadToEnd is doing the right thing… you are already at β€œthe end”. There are zero characters left to read. And that the reason that it’s unreliable is because you have two things messing with the same stream, which is a big no-no.
canton7
canton7β€’3d ago
It isn't. See the screenshots

Did you find this page helpful?