Peek() hangs

I’m trying to do something very simple but it’s not working. I have a small text file on the SD card that I read and print but the code hangs in the peek function when the end of file is reached. Any idea what I’m doing wrong? Should I be doing this some other way?

using System;
using System.Threading;
using System.IO;
using System.IO.Ports;
using System.Text;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.System;
using GHIElectronics.NETMF.Hardware;

namespace FPII_StreamReader
    public class Program

        public static void Main()
            const string path = @ "\SD\weather.txt";
            PersistentStorage sdPS;
            // *** Turn off garbage collection output ***

            // Blink board LED

            bool ledState = false;

            OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);

            bool sdExists = PersistentStorage.DetectSDCard();

            if (sdExists)
                // Create a new storage device
                sdPS = new PersistentStorage("SD");

                // Mount the file system

                if (File.Exists(path))
                    using (StreamReader sr = new StreamReader(path))
                        while (sr.Peek() >= 0)



            while (true)
                // Sleep for 500 milliseconds

                // toggle LED state
                ledState = !ledState;


Sorry I don’t understand. Are you suggesting there’s something I could place in the while statement other than sr.peek()>0?

Yes that works. Thank you.

I’m not sure it’s useful as it turns out.

I changed the peek line to

while (!sr.EndOfStream)

and get some strange behavior. If I read a record before getting to the while statement everything works fine.

If I don’t read a record first I get an exception but the debugger can’t tell me where. I assumed it was in the EndOfStream function but if I set a breakpoint anywhere in the program I no longer get an exception but EndOfStream is true so no records are read.

File is a simple text file with each line ending in \r\n. I am able to read it fine if I read a single record (i.e. string ss=sr.ReadLine(); ) before entering the while loop. Otherwise an exception is raised unless I stop the code using the debugger and then continue.

Exception System.IO.IOException - CLR_E_FILE_IO (4)

#### Message: 
#### Microsoft.SPOT.IO.VolumeInfo::.ctor [IP: 0000] ####
#### Microsoft.SPOT.IO.RemovableMedia::MessageHandler [IP: 0022] ####

A first chance exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
An unhandled exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll

If I set a breakpoint on the line “if (File.Exists(path))” or anywhere after it and simply continue from that point the exception doesn’t occur.

Well that did get rid of the exception. That’s good to know. Thanks again.