Trouble

Apr 26 2010 4:15 PM

Hey i´m having trouble with some programming in C#. I want to read a file every time it changes.
try
{
using (FileStream file = new FileStream(@"C:\myFliles\test.txt", FileMode.Open, FileAccess.Read))
{
StreamReader sr = new StreamReader(file);
string s = "";
while ((s = sr.ReadLine()) != null)
{
if (sr.EndOfStream)
{
sr.Dispose();
file.Dispose();
sr.Close();
file.Close();

MessageBox.Show(s);
break;
}
}

}
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);

The this code works fine the first time the file changes, but the second time i change the file an exception is raised that says that the file already are in use by another process. How can I solve this problem?
 
// Linus

Answers (8)