XMLReader file used by another process

H

heday60

I've got this application that watches a directory for XML's and then
parses them into a DB. It then moves them based on if the XML was
succesful or a bad XML...

foreach(FileInfo file in dir.GetFiles("*.xml"))
{
try
{
ParseXML(file.FullName);
}
catch
{
FilesToMoveError.Add(file.FullName);
break;
}
FilesToMove.Add(file.FullName);
fileCount++;
}
}

moveFiles();

The parsing is as such, and this is my latest iteration to attempt to
make sure hte reader is closed.

public void ParseXML(string filename)
{
XmlReader xmlReader = null;
try
{
FileStream stream = new FileStream(filename,
FileMode.Open,FileAccess.Read);
xmlReader = new XmlTextReader (stream);

while(!xmlReader.EOF)
{
xmlReader.Read();
switch (xmlReader.NodeType)
{
}
}
}
catch (XmlException e)
{
DebugLog(null, "Error parsing XML " + e.Message);
}
finally
{
if (xmlReader != null)
{
xmlReader.Close();
}
}
}

When I hit the file.move in my move files function it gives me the
exception "file used by another process"

What am I missing??
Thanks
 
H

Harry

I just added the filestream stuff, it was hanging before that, but
I'll give it a shot.

Thanks for the reply.
 
H

Harry

Didn't help.

added the file stream declaration near the reader and then
if (xmlReader != null)
{
xmlReader.Close();
stream.close();
}
Still got the error.
 
L

Luc E. Mistiaen

Be carefull that a file appear in the directory as soon as it is *being*
created. That means that the file creator may not have closed it when you
are trying to move it.
One way to avoid this might be to ask the creator to create/copy the file
with another extension and rename it when the file is ready for use.

/LM
 
H

Harry

That's very true Luc, and it's what we do on several other options,
but this process is supposed to be kicked off after the files are
moved into the directory, in the instance that I'm testing, the file
has been in the directory for a while, and when I try to move it using
the code snippet, it throws the "in use" error on the move command.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top