Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you reset a C# .NET TextReader cursor back to the start point?

Tags:

I have a method that takes either a StringReader instance (reading from the clipboard) or a StreamReader instance (reading from a file) and, at present, casts either one as a TextReader instance.

I need it to 'pre-read' some of the source input, then reset the cursor back to the start. I do not necessarily have the original filename. How to I do this?

There is mention of the Seek method of System.IO.Stream but this is not implemented in TextReader, although it is in StreamReader through the Basestream property. However StringReader does not have a BaseStream property

like image 884
Brendan Avatar asked May 06 '09 19:05

Brendan


People also ask

Where is the AC reset button?

Look for it first around on the exterior of your machine, especially along the bottom edge near the ground. An AC's reset button is typically red and visible, so it should be easy to spot. If you don't see a reset button on the outside, it could be located inside the unit behind the service panel.

Is there a way to reset an AC unit?

Once you've established there is power, you can restart your AC using the reset button. Some air conditioning systems have a reset switch, which is a small, red button. Typically, the button will be on the outdoor unit.

How long does an AC take to reset?

After you've turned the system back on, it will take a bit of time for the air conditioner's internal circuitry to reset. It usually takes about 30 minutes, so sit tight until then.


2 Answers

It depends on the TextReader. If it's a StreamReader, you can use:

sr.BaseStream.Position = 0; sr.DiscardBufferedData(); 

(Assuming the underlying stream is seekable, of course.)

Other implementations of TextReader may not have a concept of "rewinding", in the same way that IEnumerable<T> doesn't. In many ways you can think of TextReader as a glorified IEnumerable<char>. It has methods to read whole chunks of data at a time, read lines etc, but it's fundamentally a "forward reading" type.

EDIT: I don't believe StringReader supports any sort of rewinding - you'd be better off recreating the StringReader from the original string, if you can. If that's not feasible, you could always create your own TextReader class which proxies all the "normal" calls to another StringReader, but recreates that proxy instance when it needs to.

like image 93
Jon Skeet Avatar answered Oct 21 '22 22:10

Jon Skeet


If it is a StreamReader, and if that stream supports seeking, then:

        reader.BaseStream.Seek(0, SeekOrigin.Begin);         reader.DiscardBufferedData(); 

However, this is not possible on arbitrary TextReaders. You could perhaps read all of it as a string, then you can use StringReader repeatedly?

like image 29
Marc Gravell Avatar answered Oct 21 '22 22:10

Marc Gravell