新しいファイルのフォルダーを監視し、プロセスを実行する Windows サービスがあります。ただし、ファイルを監視対象フォルダーにドロップするたびに、サービスがクラッシュします。これが私が受け取っている例外です:
アプリケーション: フレームワーク バージョン: v4.0.30319 説明: 未処理の例外が発生したため、プロセスが終了しました。例外情報: System.IO.IOException スタック: System.IO._ Error.WinIOError(Int32, System.String) at System.IO. Service.Service.OnChanged(System.Object, System. IO.FileSystemEventArgs) で System.IO.FileSystemWatcher.OnCreated (System.IO.FileSystemEventArgs)
System.IO.FileSystemWatcher.NotifyFileSystemEventArgs (Int32、System.String) で System.IO.FileSystemWatcher.CompletionStatusChanged (UInt32、UInt32、System.Threading.NativeOverlapped*) で System.Threading._IOCompletionCallback.PerformIOCompletionCallback (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()
{
}