I am using File Watcher (.NET) and always getting an below error- file is being used by another process.
The above error only comes in Network sharing, i.e. Watch Directory is shared and if someone puts the file into that directory then only this error occurs. It works fine when I put the file from my m/c to watch directory(locally).
This is how my below code looks, the error comes in first line itself. I have tried all the solutions given on net but nothing seems to working. The only solution worked when I put Thread.Sleep(500), but after that it is unable to pick the next file. Any suggestion would be a great help.
try
{
using (Stream stream = File.Open(xmlPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
XmlTextReader xmlTextReader = new XmlTextReader(stream);
document = XDocument.Load(xmlTextReader);
........ // my code
xmlTextReader.Close();
stream.Close();
}
}
catch (Exception exception)
{
EventLog.WriteEntry("Application", exception.ToString(), EventLogEntryType.Error);
}