I created a class that opens a file like so.

 

class FileReader

{

    public void ReadTextFile(string filePath)

    {

        using (TextReader reader = File.OpenText(filePath))

        {

           frm1.textBox1.Text = frm1.textBox1.Text + reader.ReadLine() +
Environment.NewLine;

        }

        File.Delete(filePath);

    }

}

 

This works great when calling it from a button click event

 

private void button2_Click(object sender, EventArgs e)

{

    File.Copy(fileSource, fileDestination);

}

 

This also works if I use a timer to trigger the FileReader Class. 

 

BUT, if I use a FileSystemWatcher Control to trigger the FileReader class it
only works the first time.

After that it says the file is in use by another process.

Even though the old file is deleted and the new file is not open anywhere.

I don't think it is a timing issue because I can wait several seconds
between and still get the same message.

 

Can anyone offer me any insights as to why this is happening?

I can live with using the timer control, but would rather use the
FileSystemWatcher

 

 

Greg

 

 

 

 

Reply via email to