C# FIleSystemWatcher IOException
我有一个Windows服务,它监视文件夹中的新文件并运行进程。但是,每次我将文件放入受监视的文件夹时,服务都会崩溃。以下是我收到的例外情况: 应用程序:框架版本:v4.0.30319说明:流程 由于未处理的异常而终止。例外信息: System.IO.IOException堆栈:位于System.IO.\u Error.WinIOError(Int32, System.IO中的System.String。_Error.WinIOError()位于 System.IO.File.Move(System.String,System.String)位于 位于的Service.Service.Process(System.String,System.String) Service.Service.OnChanged(System.Object, System.IO.FileSystemEventArgs)位于 System.IO.FileSystemWatcher.onCreate(System.IO.FileSystemEventArgs)C# FIleSystemWatcher IOException,c#,process,stack-trace,filesystemwatcher,windows-defender,C#,Process,Stack Trace,Filesystemwatcher,Windows Defender,我有一个Windows服务,它监视文件夹中的新文件并运行进程。但是,每次我将文件放入受监视的文件夹时,服务都会崩溃。以下是我收到的例外情况: 应用程序:框架版本:v4.0.30319说明:流程 由于未处理的异常而终止。例外信息: System.IO.IOException堆栈:位于System.IO.\u Error.WinIOError(Int32, System.IO中的System.String。_Error.WinIOError()位于 System.IO.File.Move(Syste
在System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32, 系统字符串)在 System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32,UInt32, System.Threading.NativeOverlapped*)在 System.Threading.\u IOCompletionCallback.PerformiCompletionCallback(UInt32, UInt32,System.Threading.NativeOverlapped*) 一切都很好,然后突然,每次我使用它时,它都会崩溃。我不记得改变了什么会导致这种情况。这是我的密码:
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()
{
}
我怀疑在某些情况下,进程仍然会锁定文件。只是想知道,你为什么不打电话:
process.Close(); // Frees all the resources that are associated with this component
在WaitForExit()之后。为什么不在确保进程与0一起存在后执行以下检查,如下所示
if (process.ExitCode == 0)
{
process.Close();
process.Dispose();
if (File.Exists(path+filename) && !IsFileLocked(fileInfo) && Directory.Exists(cleanPath))
File.Move(path, cleanPath);
}
我怀疑在您尝试移动时,扫描过程是否仍在隔离文件
参考:
当然,您应该适当地处理else条件…我在创建进程之前添加了Thread.Sleep(5000)。这很有效,但扫描每个文件需要5秒的时间。而且,正如人们所说的那样,这感觉有点骇人。可能是另一个进程,比如说手动启动的扫描仪的另一个实例,在您尝试移动文件时仍然保持对该文件的锁定吗 您应该使用查看哪些进程正在访问该文件
无论如何,您不扫描新文件,而是始终扫描整个文件夹L:\Test。这是你的意图吗 使用MpCmdRun时,似乎需要两个技巧:
MpCmdRun
时,在参数列表中包括-disablereception
。这会使流程运行得非常快,并通过StandardOutput
报告结果“未发现威胁”
,以了解文件是否干净。如果没有,请查找以“威胁”开头的一行,然后阅读它以查看发现了什么病毒运行服务的帐户是否仍有权在移动操作的目标目录中创建文档?捕获Process方法中的异常。它把文件扔进去了。查看异常是什么,以了解如何处理它。用try/catch块环绕引发异常的行,并输出有关上下文的信息。可能文件仍被您的拖放操作锁定。您应该将
using
关键字与TextWriter一起使用。您还应该处理您的进程对象。@Stuart。是的,该帐户确实拥有该文件夹的权限。我很懒。我现在要补充一点。