I need some help with a file read issue.

I have a watched folder that reads the text file and writes it to a standard
textbox.

For testing purposes I have a file called text.txt on my desktop that I copy
to the watched folder.

The first time I copy the file to the watched folder it reads it fine and
deletes it from the watched folder.

Then if I take that file and copy it into the folder again I get the
following error.

 

The process cannot access the file . <path to the file in the watched
folder> . because it is being used by another process.

Here is my test code. 

 

string filePath = Path.Combine(QRWatchedFolder, e.Name);

FileStream file = File.Open(filePath, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);

StreamReader SR = new StreamReader(file);

textBox1.Text = textBox1.Text + SR.ReadToEnd() + Environment.NewLine;

SR.Close();

File.Delete(filePath);

 

Now since the file is deleted , when it is copied again from the desktop why
does it say the file is being used?

Am I not closing my StreamReader properly?

 

Any help would be greatly appreciated.

Greg

Reply via email to