与FileSystemWatcher的文件访问错误当多个文件添加到目录 [英] File access error with FileSystemWatcher when multiple files are added to a directory

查看:281
本文介绍了与FileSystemWatcher的文件访问错误当多个文件添加到目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个FileSystemWatcher的一个问题,当多个文件被放入监控目录。我想,只要它被放置在目录解析文件。典型地,第一文件解析细,但添加一个第二文件的目录导致访问问题。偶尔,第一文件甚至不解析。世界上只有一个运行,看这个目录应用程序。最终,这一过程将在多台机器上运行,它们将密切关注的共享目录,但是在将数据导入到数据库中只有一台服务器可以解析每个文件,没有主键。

I am running into an issue with a FileSystemWatcher when multiple files are placed into the watched directory. I want to parse the file as soon as it is placed in the directory. Typically, the first file parses fine, but adding a second file to the directory causes an access issue. Occasionally, the first file doesn't even parse. There is only one application running and watching this directory. Eventually, this process will be running on multiple machines and they will be watching a shared directory but only one server can parse each file as the data is imported into a database and there are no primary keys.

下面是FileSystemWatcher的code:

Here is the FileSystemWatcher code:

public void Run() {
  FileSystemWatcher watcher = new FileSystemWatcher("C:\\temp");
  watcher.NotifyFilter = NotifyFilters.FileName;
  watcher.Filter = "*.txt";

  watcher.Created += new FileSystemEventHandler(OnChanged);

  watcher.EnableRaisingEvents = true;
  System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}

然后就是解析文件的方法:

Then the method that parses the file:

private void OnChanged(object source, FileSystemEventArgs e) {
  string line = null;

  try {
    using (FileStream fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.None)) {
      using (StreamReader sr = new StreamReader(fs)) {
        while (sr.EndOfStream == false) {
          line = sr.ReadLine();
          //parse the line and insert into the database
        }
      }
    }
  }
  catch (IOException ioe) {
    Console.WriteLine("OnChanged: Caught Exception reading file [{0}]", ioe.ToString());
  }

在移动第二个文件,它正在迎头赶上

When moving the second file, it is catching

System.IO.IOException:该进程无法访问该文件'C:\\ TEMP \\ TESTFILE.TXT'。因为它正被另一个进程使用

System.IO.IOException: The process cannot access the file 'C:\Temp\TestFile.txt' because it is being used by another process.

我希望看到这个错误,如果它是在多台机器上运行,但它只是一台服务器上运行了。不应该使用这个文件是另一个进程 - 我让他们创建,并将它们复制到该目录中的应用程序运行时

I would expect to see this error if it was running on multiple machines, but it is only running on one server for now. There shouldn't be another process using this file - I have them created and copy them into the directory when the application is running.

这是正确的方式来设置FileSystemWatcher的?我怎么能看到有这个文件的锁?为什么它不分析这两个文件 - 我必须关闭的FileStream?我想保持FileShare.None选项,因为我只想要一台服务器来解析文件 - 即获取到的文件首先分析它的服务器

Is this the proper way to set up the FileSystemWatcher? How can I see what has the lock on this file? Why doesn't it parse both files - do I have to close the FileStream? I want to keep the FileShare.None option because I only want one server to parse the file - the server that gets to the file first parses it.

推荐答案

这种方法的一个典型的问题是,虽然被触发事件的文件至今仍然被复制。很明显,因为该文件复制过程中被锁定,您将得到一个异常。一个例外是对大型文件的可能。

A typical problem of this approach is that the file is still being copied while the event is triggered. Obviously, you will get an exception because the file is locked during copying. An exception is especially likely on large files.

作为一种解决方法,你可以先复制该文件,然后将其重命名,听听重命名的事件。

As a workaround you could first copy the file and then rename it and listen to the renaming event.

或者另一种选择是将有一个while循环检查是否该文件可以在写访问被打开。如果可以,你会知道,复制已经完成。 C#code可能看起来像这样(在生产系统中,你可能希望有重试或超时,而不是,而(真)的最大数量):

Or another option would be to have a while loop checking whether the file can be opened with write access. If it can you will know that copying has been completed. C# code could look like this (in a production system you might want to have a maximum number of retries or timeout instead of a while(true)):

/// <summary>
/// Waits until a file can be opened with write permission
/// </summary>
public static void WaitReady(string fileName)
{
    while (true)
    {
        try
        {
            using (Stream stream = System.IO.File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                if (stream != null)
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} ready.", fileName));
                    break;
                }
            }
        }
        catch (FileNotFoundException ex)
        {
            System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));
        }
        catch (IOException ex)
        {
            System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));
        }
        catch (UnauthorizedAccessException ex)
        {
            System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));
        }
        Thread.Sleep(500);
    }
}

另一种方法将是复制完成之后放置一个小的触发文件的文件夹中。您FileSystemWatcher的会听只触发文件。

Yet another approach would be to place a small trigger file in the folder after copying is completed. Your FileSystemWatcher would listen to the trigger file only.

这篇关于与FileSystemWatcher的文件访问错误当多个文件添加到目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆