Hey guys thanks for all of your input.
After reading the replies and sitting and playing with this for a bit I
realized how simple the issue really is and found an easy way to resolve it.
In my program I am CREATING a text file.
Well if you look at the .Net steps when you create a text file, the first
thing is you CREATE the file and THEN you add the text to it.
Well the FileSystemWatcher is apparently VERY diligent to say the least.
As soon as the File is created is when this bad boy triggers.
And since that is the first step in the File Creation Method the file is
there, but not ALL there, it is still being created.
But the issue does not happen if I create the file elsewhere and then COPY
it to the Watched Folder.
Just my two cents worth (1 cent after taxes).
And thanks again for everyone's input!
Greg
_____
From: [email protected]
[mailto:[email protected]] On Behalf Of brajkishor tiwari
Sent: Monday, October 04, 2010 9:33 AM
To: [email protected]
Subject: Re: [DotNetDevelopment] Re: Problems with Reading a file when using
FileSystemWatcher
On Fri, Oct 1, 2010 at 11:20 AM, Pratiksha Saxena
<[email protected]> wrote:
Actually file remains in use .You can check this by using FileFolderUnlocker
software.
Now in your code check whenever problem comes use System.Diagnostics
Process.kill.
It will work.
Pratiksha
On Fri, Oct 1, 2010 at 11:09 AM, Cerebrus <[email protected]> wrote:
***Referenced from the other thread in which Greg posted:***
Greg Hile wrote:
----------------------
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