I'm using the fileSystemWatcher in a windows service to detect when an xml file is dropped into an outbox directory. If the file dropped in there is not an xml file I want to delete it. Most of the time when I drop a small (0 - 1K) file in there that does not have a .xml extension it is deleted with no problem. If the file is larger (>5-6K) I get an exception stating that file access is denied when it tries to delete.
CODE
string fileName = e.Name.ToString();
FileInfo newFile = new FileInfo(fileName);
string fileExt = newFile.Extension;
if (fileExt != ".xml")
{
newFile.Delete();
File.Delete(outboxPath + "\\" + fileName);
using (StreamWriter sw = File.AppendText(logPath))
{
sw.WriteLine(DateNow + "- " + e.Name.ToString() + " is an invalid file type. Removed from outbox");
sw.Close();
sw.Dispose();
}
}
I either need to be able to check to see if the file is open or be able to close it before deleting but have not been able to figure out how to do either...