Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 首次使用FileSystemWatcher监视文件夹失败_C#_File Io_Windows Services_Filesystemwatcher - Fatal编程技术网

C# 首次使用FileSystemWatcher监视文件夹失败

C# 首次使用FileSystemWatcher监视文件夹失败,c#,file-io,windows-services,filesystemwatcher,C#,File Io,Windows Services,Filesystemwatcher,我每天都会收到来自不同用户的许多xml文件。这些文件被FTPed到我驱动器上的一个文件夹中,比如D:\batch。今天,我用一个计划任务检查这些文件,该任务启动ASP.NET页面,处理找到的文件,回答上传者的问题,然后结束。这个任务每15分钟运行一次,但是上传者希望更快地得到答案,所以我正在尝试完成这个任务。我创建了一个监控文件夹的Windows服务,当创建一个文件时,它会对其进行处理 我遵循了几个不同的指南,尤其是这一个 对于第一个文件,它可以工作,但每次添加第二个文件时,它都会崩溃: Exc

我每天都会收到来自不同用户的许多xml文件。这些文件被FTPed到我驱动器上的一个文件夹中,比如D:\batch。今天,我用一个计划任务检查这些文件,该任务启动ASP.NET页面,处理找到的文件,回答上传者的问题,然后结束。这个任务每15分钟运行一次,但是上传者希望更快地得到答案,所以我正在尝试完成这个任务。我创建了一个监控文件夹的Windows服务,当创建一个文件时,它会对其进行处理

我遵循了几个不同的指南,尤其是这一个

对于第一个文件,它可以工作,但每次添加第二个文件时,它都会崩溃:

Exception Info: System.IO.IOException
Stack:
   at System.IO.__Error.WinIOError(Int32, System.String)
   at System.IO.File.InternalCopy(System.String, System.String, Boolean, Boolean)
   at System.IO.File.Copy(System.String, System.String)
   at FileMonitor.FilKigger.Watcher_Created(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*)
这是怎么回事

一些代码:

protected override void OnStart(string[] args)
    {
        FileSystemWatcher Watcher = new FileSystemWatcher();
        Watcher.Path = "D:\\FTP\\Batch\\";
        Watcher.IncludeSubdirectories = true;
        Watcher.Created += new FileSystemEventHandler(Watcher_Created);
        Watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);
        Watcher.EnableRaisingEvents = true;
    }
这里只是将文件复制到新文件夹的代码

void Watcher_Created(object sender, FileSystemEventArgs e)
    {
        File.Copy(e.FullPath, "D:\\newFolder\\" + e.Name);
        Lg.WriteEntry("File moved", EventLogEntryType.Information);
    }

捕获异常只会留下文件,但当然会保持服务运行。

更改了监视程序以查找“\u finish.txt”-文件,用户在上传真实文件后发送这些文件

Watcher = new FileSystemWatcher(ftpFolder, "*_finish.txt");

然后将_finish替换为“”,我得到了保证上传完成的真实文件

您是否提供异常消息,而不仅仅是它的类型?如果有,请也发布内部异常..该异常在哪里引发(哪行代码)?请记住,创建文件时会触发创建的事件。因此,如果它是一个仍在传输的大文件,那么该文件将被创建(但尚未完成),并且您希望开始复制它,但该文件实际上还不存在。听起来有点模糊。。希望你得到它。可能这也是您的问题。在System.IO.IO.Error.WinIOError(Int32 errorCode,String maybeFullPath)在System.IO.File.InternalCopy(String sourceFileName,String destFileName,Boolean overwrite,Boolean checkHost)在System.IO.File.Copy(String sourceFileName,String destFileName)在FileMonitor.FilKigger.Watcher\u创建(Object sender,FileSystemEventArgs eThere在您发布的堆栈跟踪中没有足够的信息。异常的HResult值是多少?除此之外,这是完全常见的。您不能在创建文件后立即复制该文件,创建该文件的应用程序仍然打开该文件。您必须等待。