473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

FileWatcher

Hello All.

I have a filewatcher object which looks at a shared folder and when a
new XML file is placed in this folder the on create method calls a
method which reads the XML file and based on the content writes a new
XML file to the same directory.

The problem i have is as follows.

If i place 10 XML files in the directory name

test1.xml
test2.xml
etc
test10.xml

then i will get 10 response XML files, BUT only on the first attempt.

If i then paste another 10 XML files in the directory

test11.xml
test12.xml
etc
test20.xml

i will only receive 9 XML response files, this is because the first XML
file from the new batch (test11.xml in this case) always fails. I
always get an exception thrown when i try to read the first file, the
rest all work, but if i repeat the process it happens again, excpetion
is thrown when i try to read the first XML file, the next 9 read
correctly and then get a response.

I dont see why this is happening, i make sure to close the
IO.filestreams and it doesnt happend for every read, i make a new
object each time the create method fires (which is 10 times for this
example) so if anything it should fail 10 times or never.
My code is as follows:

private void OnXMLFileCreated(object source, FileSystemEventArgs e)
{
XMLFileEventMng test = new XMLFileEventMng();
try
{
test.HandleEvent(e.FullPath.ToLower());
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

In the XMLFileEventMng when i do

System.IO.FileStream fsReadXml = new System.IO.FileStream (xmlFileName,
FileMode.Open);

I receive this:

System.IO.IOException: The process cannon access the file "test11.xml"
because it is being used by another process.
If anyone has any ideas on this i would really appreciate them.

Thanks

Adam

Jan 26 '07 #1
3 2846

I Have found a workaround for this as follows:

try
{
fsReadXml = new System.IO.FileStream (xmlFileName, FileMode.Open);
}
catch(Exception ex)
{
fsReadXml = new System.IO.FileStream (xmlFileName, FileMode.Open);
}

This seems to solve the problem of the error being thrown, but i would
much rather figure out what is actually causing the Exception as this
is a very messy way to deal with this.

thanks

Adam

On Jan 26, 1:27 pm, "ba.hons" <ba.h...@gmail.comwrote:
Hello All.

I have a filewatcher object which looks at a shared folder and when a
new XML file is placed in this folder the on create method calls a
method which reads the XML file and based on the content writes a new
XML file to the same directory.

The problem i have is as follows.

If i place 10 XML files in the directory name

test1.xml
test2.xml
etc
test10.xml

then i will get 10 response XML files, BUT only on the first attempt.

If i then paste another 10 XML files in the directory

test11.xml
test12.xml
etc
test20.xml

i will only receive 9 XML response files, this is because the first XML
file from the new batch (test11.xml in this case) always fails. I
always get an exception thrown when i try to read the first file, the
rest all work, but if i repeat the process it happens again, excpetion
is thrown when i try to read the first XML file, the next 9 read
correctly and then get a response.

I dont see why this is happening, i make sure to close the
IO.filestreams and it doesnt happend for every read, i make a new
object each time the create method fires (which is 10 times for this
example) so if anything it should fail 10 times or never.

My code is as follows:

private void OnXMLFileCreated(object source, FileSystemEventArgs e)
{
XMLFileEventMng test = new XMLFileEventMng();
try
{
test.HandleEvent(e.FullPath.ToLower());
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}

}In the XMLFileEventMng when i do

System.IO.FileStream fsReadXml = new System.IO.FileStream (xmlFileName,
FileMode.Open);

I receive this:

System.IO.IOException: The process cannon access the file "test11.xml"
because it is being used by another process.

If anyone has any ideas on this i would really appreciate them.

Thanks

Adam
Jan 26 '07 #2

ba.hons wrote:
I have a filewatcher object which looks at a shared folder
I always get an exception thrown when i try to read the first file
I had a similar problem recently. In my case it was because the file
creation event that the app receives seems to happen before the file
handle is closed by whatever is generating them. I'm guessing a delay
is more likely on a share.

To work around this I implemented a waiting system (if anyone has any
better suggestions feel free) along the following lines:

public static bool CheckFileAvailable(string filePath)
{
bool isAvailable = false;
try
{
using (FileStream inputStream = File.Open(filePath,
FileMode.Open, FileAccess.Read, FileShare.None))
{
isAvailable = true;
}
}
catch (IOException)
{
isAvailable = false;
}
return isAvailable;
}

This is just a single 'available?' check, and In my case I expanded
this to a 'try 3 times' type loop with a Thread.Sleep() call on each
iteration. Note that only IOException is caught rather than Exception,
to allow other exceptions to propagate up through the call stack
properly.

Jan 26 '07 #3
Hi,

"Bobbo" <ro************@choicequote.co.ukwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
|
| ba.hons wrote:
|
| I have a filewatcher object which looks at a shared folder
| I always get an exception thrown when i try to read the first file
|
| To work around this I implemented a waiting system (if anyone has any
| better suggestions feel free) along the following lines:

I also got into the same problem, I did something else, not sure if it work
in your environment but anyway here it goes

As soon as I receive an event I put it in a Sync'ed queue. Then I have a
worker thread devoted to process those request, it;s mainly a loop where it
check if there is something in the queue if not it goes to sleep X seconds
until it poll again the queue, if there is something in the queue I use Peek
to see if I can access the file. If I get access error probably is cause the
originator is still writting it (they are some big XML files). so I go back
to sleep. Once I can read it I just DeQueue it and do the processing.

Now the FileSystemWatcher may generate more than one event for a
"modification" in my case I can avoid reprocessing cause I delete the file
after processing. so the first thing I do when I Dequeue it is checking if
the file still exist.


--
Ignacio Machin
machin AT laceupsolutions com

Jan 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lloyd Sheen | last post by:
Ok here is problem. Steps: 1. Get filename from Directory.GetFiles collection 2. Store in SQL Server the name as string 3. Another program implements FileWatcher 4. Change the file who's...
4
by: Ron King Jr | last post by:
I have a small problem with my windows service. The first time I start the service everything works the way it's suppose to, but randomly the service fails. The jist of the service is to use the...
0
by: andre | last post by:
I’m trying to use the Filewatcher class in Visual Studio.net. I’m watching an FTP folder looking for a File. The File size is about 200 to 300 megs. When the file is done uploading, I need...
1
by: PeterNZ | last post by:
Hi all, I developped an C# app which is using FileSystemWatcher. If a file is created in a specific folder, it opens the file and does some processing. This functionality works without...
6
by: Troy Murphy | last post by:
The help file for Visual Studio .NET version 1 had a Walkthru for installing a Windows Service that would monitor a folder. Version 1.1 does not seem to have that example anymore. Could someone...
0
by: adam_scheich | last post by:
When creating a file in excel and saving it to a directory that is being watched by filewatcher using Save As, the filewatcher created event doesn't fire. It works fine for text files, but for...
2
by: Roger Twomey | last post by:
I am working on a filewatcher application. The premis is: User uploads an xml file onto the web server the filewatcher app sees the xml file filewatcher app reads the file and inserts...
1
by: ba.hons | last post by:
Hello, I have a file watcher object which i use to check a directory for XML files. When an XML file is placed in the directory i check the filename, to make sure it for me, then parse the...
0
by: tshad | last post by:
I have a filewatcher program that is working syncronously (which is how I want it to work). But my program will handle whatever files are in the folder at the time it runs. So when I drop 15...
7
by: tshad | last post by:
What exactly is FileWatcher doing? When you drop 100 files in a folder it is watching, it normally will fire of the event 100 times. In my case, I do all my processing on the first event so I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.