title: Can I prevent a StreamReader from locking a text file whilst it is in use?
body: <p>The StreamReader locks a text file whilst it is reading it.<br>
  Can I force the StreamReader to work in a "read-only" or "non locking" mode?</p>
  
  <p>My workaround would be to copy the file to a temp location and read it from there but I would prefer to use the StreamReader directly if possible.
  Any alternative suggetions?</p>
  
  <p>Background:<br>
  I've written a small app to get some stats out of a log file. This file is constantly being updating (several times a second) by an outside program lets call AAXXYY. </p>
  
  <p>Reviewing the output suggests that my app may be locking the file and preventing AAXXYY from writing. </p>
  
  <p>This is what I'm doing</p>
  
  <pre><code>    private void btnGetStats_Click(object sender, EventArgs e)
      {
          int countStarts = 0;
          int countEnds = 0;
  
          IList&lt;string&gt; sessions = new List&lt;string&gt;();
  
          using(StreamReader stRead = new StreamReader(openFileDialog1.FileName,Encoding.Unicode))
          {
              while(!stRead.EndOfStream)
              {
                  string line = stRead.ReadLine();
                  if(line.Contains("Session start"))
                  {
                      countStarts++;
                      sessions.Add(line.Substring(line.IndexOf("["), line.LastIndexOf("]") - line.IndexOf("[")));
                  }
                  if (line.Contains("Session end"))
                  {
                      countEnds++;
                      sessions.Remove(line.Substring(line.IndexOf("["), line.LastIndexOf("]") - line.IndexOf("[")));
                  }
              }
          }
  
          txtStarts.Text = countStarts.ToString();
          txtEnds.Text = countEnds.ToString();
          txtDifference.Text = (countStarts - countEnds).ToString();
  
          listBox1.DataSource = sessions;
      }
  </code></pre>