0

I have this weird problem with a class I implemented for reading files that are appended multiple time per second and are located in network shares. During the first iteration everything is fine and at the end of the reading I keep the last position in database. During the second pass however, the filestream does not "see" the actual size of the appended file but it remembers the previous size.

Could it be the fact that I am reading from network shares? Thank you in advance for your feedback.

public long? ReadFromFile(string Path, long lastposition, int ClusterId, Startup ST)
    {
        QuickFix.FieldMap QFM = new QuickFix.FieldMap();


        FileStream fs   =new FileStream(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite,1024,true);



        StreamReader sr = new StreamReader(fs);

        long CurrentPosition = lastposition;

        sr.BaseStream.Seek(lastposition, SeekOrigin.Begin);
        string line;
        long i = 0;

        while ((!sr.EndOfStream))
        {
            line = sr.ReadLine();
            CurrentPosition = CurrentPosition + System.Text.ASCIIEncoding.ASCII.GetByteCount(line) + 1;
        }
        lastposition = CurrentPosition;

        sr.Close();
        sr.Dispose();
        fs.Flush();
        fs.Close();
        fs.Dispose();

      return lastposition;
    }

The first pass is always successfull. The second pass does not go through the loop b/c the lastposition where I am moving the pointer remains the same even though the actual file has grown up.

UPDATE: The file is located in a solaris server.

epapas
  • 1
  • 1
  • What do you mean by first pass? First pass of the while loop? Or first pass of this function as a whole? Where is the code that calls this function? – Trevor Elliott Oct 16 '13 at 19:27
  • 1
    Don't bother with Close, Dispose, and Flush. Just put your stream in a using block. [Like this](http://stackoverflow.com/questions/614959/using-the-using-statment-in-c-sharp) – tnw Oct 16 '13 at 19:31
  • This routine is called successfully every 500 ms and with debugging on I saw that it enters in the loop. I did use using with the same results. In debug mode I had a watch on fs.length and it was always the intitial one. For what it matters the file is being read from a solaris machine. – epapas Oct 16 '13 at 21:08

0 Answers0