Ok after some more testing I found that if I have 2 different files, one
called test1.txt and the other called test2.txt and I wrap the code in a
using statement I still have the same issue.

Now I am deleting the file successfully, but 

I have Googled this and every solution doesn't work for me.

Here is the super short test code.

 

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

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

{

textBox1.Text = textBox1.Text + reader.ReadToEnd();

      reader.Close();

}

File.Delete(filePath);  

 

The code is in a SystemFileWatcher_Created Event.

 

Thanks in advance!

 

Greg 

 

  _____  

From: [email protected]
[mailto:[email protected]] On Behalf Of Greg Hile
Sent: Wednesday, September 29, 2010 8:56 PM
To: [email protected]
Subject: [DotNetDevelopment] File Read Issues

 

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