Despite other posts there is nothing wrong with reading from a file and writing into it "at the same time". The key to understanding the problem is the MODE which is used when OPENING the file.
have a look at a more explicit variant of your code:
using (FileStream fileStream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
using (StreamReader rd = new StreamReader(fileStream))
{
using (StreamWriter wr = new StreamWriter(fileStream))
{
string line = null;
while ((line = rd.ReadLine()) != null)
{
if (String.Compare(line, active_user) == 0)
{
//do nothing
}
else
{
wr.WriteLine(active_user);
//If you flush what you write then you will see it via the reader.
//Comment out for better performance. Keep in mind, that writing
//into a file will lead to occasional flush which affects the
//reader. Implicit Flush happens once you write more than the
//buffer of the writer suggests.
wr.Flush();
}
}
}
}
}
As you can see, it is different from your code in the way the file is opened. I just prefer to explicitly specify the mode I open file with.
It appears that the StreamReader implicitly opens the file without "sharing" i.e. does not allow others to open it and this is OK because it wants to protect you from getting unexpected results. Hereby this implicit method does not fit your slightly more "complicated" scenario. Opening the file explicitly does the trick.
Note that File.Open allows you to open a file and still allow other threads (other applications) open the file. It's all up to you.
Now to the logic of your code.
StreamReader reads the stream from the beginning. StreamWriters appens to the end of contents of the stream. Both keep and maintain their own stream positions. Thus the code will append a line containing the "active_user" for each line of your file which does not contain "active_user". I can not imagine a practical reason for that.