Using TextWriter with StreamWriter and Reading/Writing Simultaneously
<p>As the title suggests, I'm attempting to read and write to a file at the same time. I have researched this topic but the answers I have found don't seem to work for me because of the circumstances in my program. I am using multiple FileSystemWatchers to track a large amount of files that are constantly passing through a flow in my network. As the files pass through each part of my flow, a text file is updated(one text file per spot in the flow) that marks the name of the file and the time it was created within the folder. It is unpredictable when files might be passing through, and when they might be writing to the tracker text files. My goal is to be able to read and write to the file simultaneously, in case a user attempts to read to from a text file that is being written to at the exact same time. How would I accomplish this?</p>

<pre><code>//Write to File
    private void WriteToFile(string info,string path,string tr)
    {
        if (!File.Exists(path+@"\"+@tr))
        {
            var myFile =
            File.Create(path + @"\" + @tr);
            myFile.Close();
            TextWriter tw = new StreamWriter(path + @"\" + @tr, true);
            tw.WriteLine(info,true);
            tw.Close();
        }
        else if (File.Exists(path + @"\" + @tr))
        {
            TextWriter tw = new StreamWriter(path + @"\" + @tr, true);
            tw.WriteLine(info);
            tw.Close();
        }
    }
</code></pre>