Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FIleSystemWatcher IOException

I have a Windows Service that monitors a folder for new files and runs a process. However, the service crashes every time I drop files into the monitored folder. Here is the Exception I am receiving:

Application: Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.IO.IOException Stack: at System.IO._Error.WinIOError(Int32, System.String) at System.IO._Error.WinIOError() at System.IO.File.Move(System.String, System.String) at Service.Service.Process(System.String, System.String) at Service.Service.OnChanged(System.Object, System.IO.FileSystemEventArgs) at System.IO.FileSystemWatcher.OnCreated(System.IO.FileSystemEventArgs)
at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32, System.String) at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32, UInt32, System.Threading.NativeOverlapped*) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)

Everything was working fine and then all of the sudden, it crashes every time I use it. I don't remember changing anything that would cause this. Here is my code:

public Service()
{
    InitializeComponent();
}

protected override void OnStart(string[] args)
{
    FileSystemWatcher watcher = new FileSystemWatcher(ConfigurationManager.AppSettings["UploadPath"]);

    watcher.Created += new FileSystemEventHandler(OnChanged);
    watcher.EnableRaisingEvents = true;
}

public static void OnChanged(object source, FileSystemEventArgs e)
{
    Process(e.Name, e.FullPath);
}

public static void Process(string fileName, string path)
{
    string newPath = Path.Combine(ConfigurationManager.AppSettings["NewPath"], fileName);

    Process process = new Process();

    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.FileName = @"C:\Program Files\Microsoft Security Client\Antimalware\mpcmdrun";
    process.StartInfo.Arguments = " -scan -scantype 3 -file L:\\Test\\";

    process.Start();

    string output = process.StandardOutput.ReadToEnd();
    process.WaitForExit();

    if (process.ExitCode == 0)
    {
        File.Move(path, cleanPath);
    }

    else
    {
        TextWriter tw = new StreamWriter(ConfigurationManager.AppSettings["FailedPath"] + fileName + ".txt");
        tw.WriteLine(output);
        tw.Close();

        File.Delete(path);
    }
}
protected override void OnStop()
{

}
like image 385
Matt Avatar asked Jun 07 '26 22:06

Matt


2 Answers

I suspect the process still has a lock on the file in some cases. Just wondering, why aren't you calling:

process.Close(); // Frees all the resources that are associated with this component

after WaitForExit().

like image 60
Ta01 Avatar answered Jun 10 '26 15:06

Ta01


Could it be that another process, say another instance of the scanner you're manually starting, still holds a lock to the file at the moment you try to move it?

You should use Process Monitor to see which processes are accessing the file.

Anyway, you don't scan the new file, you always scan the whole folder L:\Test. Is that your intention?

like image 37
CodeCaster Avatar answered Jun 10 '26 13:06

CodeCaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!