Question Get StreamWriter current position

cbreemer

Well-known member
Joined
Dec 1, 2021
Messages
184
Programming Experience
10+
I wonder if there is a way to query the current write position of a StreamWriter object ? Or rather, I just want to know if the writer has been written to at all (which would be so if the current position is non-zero). Currently I have to maintain a separate boolean for this, but I thought there just might might be a more clever way about it.
 
Not the StreamWriter itself, but you could check the Position or Length of the BaseStream exposed by the StreamWriter.
 
Not the StreamWriter itself, but you could check the Position or Length of the BaseStream exposed by the StreamWriter.
Aha ! I overlooked that BaseStream property. Rather I was looking up the hierarchy tree, getting no further than TextStream, and not seeing anything.
This should serve my purpose, I guess. Thanks !!!
 
As a side note, for object oriented programming purists, going down this approach I suggested in post #2 looks and feels like it is breaking The Law of Demeter. A cleaner (but more verbose) approach is to use the decorator pattern where the only thing the decorator does is note whether any write operations have been done on the StreamWriter yet.
 
As a side note, for object oriented programming purists, going down this approach I suggested in post #2 looks and feels like it is breaking The Law of Demeter. A cleaner (but more verbose) approach is to use the decorator pattern where the only thing the decorator does is note whether any write operations have been done on the StreamWriter yet.
Thanks and very interesting, but I'm proud to be a pragmatist rather than a purist ( my code uses the occasional goto as well 😁). The idea of having to write additional code, just to satisfy some design principle pretending to be a Law, does not much appeal to me. Especially as I can simply pick up that little piece of info I need. I might think different if I was part of a large team creating humongous software.
 
Yup, some days I setup the circular saw and edge guides, and other days I just use the handsaw following a line made by a dull pencil.
 
StreamWriter buffers writes, so you probably need to Flush before checking underlying stream (or enable AutoFlush).
 
Which then brings back around the topic of the Law of Demeter regarding not needing to know details of the implementation of an object. So not only do you need to know the details about the stream being accessible, but you also need to know about the details of the behavior.
 
Which then brings back around the topic of the Law of Demeter regarding not needing to know details of the implementation of an object. So not only do you need to know the details about the stream being accessible, but you also need to know about the details of the behavior.
Yes, this does make it a bit more debatable. Actually I decided to remove that code completely as I did not really need it on second thought. So, no danger of violating the law (or this particular one at least).

Still, it seems like a quite common problem for which there is no built-in solution. I create and open a batch file, just in case the ensuing code (a long loop over thousands of media files) might want to write a command to it (which depends on the circumstances). After code completion, I wanted to execute this batch file but only when something was indeed written to it. If not, I simply delete it. Not such a far-fetched scenario is it ? I guess I could just close the file and then examine the file size. Or just execute the file even when it might be empty - no harm done.
 
Back
Top Bottom