在发布文件锁时获得通知

时间:2016-03-24 16:39:15

标签: c# .net filesystemwatcher ntfs filelock

[使用C#和Windows作为平台]

我有一台将JPG文件写入PC中本地文件夹的相机。我想加载相机掉落的每个文件,所以我有一个FileSystemWatcher,它会在创建新图片时通知我,但相机会在文件写入时锁定文件,所以如果我尝试加载它在收到创建通知后,我得到一个例外,说文件被锁定

目前,我有一个while循环(使用Thread.Sleep)重试每0.2秒加载一次图像,但感觉有点脏。

是否有更优雅的方式等待锁被释放,所以我可以加载文件,确保它不再使用?

1 个答案:

答案 0 :(得分:1)

您将无法绕过试错法,即尝试打开文件,捕获IOException,再试一次。但是,您可以将这种丑陋隐藏在一个单独的类中,如下所示:

public class CustomWatcher
{
    private readonly FileSystemWatcher watcher;
    public event EventHandler<FileSystemEventArgs> CreatedAndReleased;

    public CustomWatcher(string path)
    {
        watcher = new FileSystemWatcher(path, "*.jpg");
        watcher.Created += OnFileCreated;
        watcher.EnableRaisingEvents = true;
    }

    private void OnFileCreated(object sender, FileSystemEventArgs e)
    {
        // Running the loop on another thread. That means the event
        // callback will be on the new thread. This can be omitted
        // if it does not matter if you are blocking the current thread.
        Task.Run(() =>
        {
            // Obviously some sort of timeout could be useful here.
            // Test until you can open the file, then trigger the CreeatedAndReleased event.
            while (!CanOpen(e.FullPath))
            {
                Thread.Sleep(200);
            }
            OnCreatedAndReleased(e);
        });
    }

    private void OnCreatedAndReleased(FileSystemEventArgs e)
    {
        CreatedAndReleased?.Invoke(this, e);
    }

    private static bool CanOpen(string file)
    {
        FileStream stream = null;
        try
        {
            stream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.None);
        }
        catch (IOException)
        {
            return false;
        }
        finally
        {
            stream?.Close();
        }
        return true;
    }
}

这个&#34;观察者&#34;可以像这样使用:

var watcher = new CustomWatcher("path");
watcher.CreatedAndReleased += (o,e) => 
{
    // Now, your watcher has managed to open and close the file,
    // so the camera is done with it. Obviously, any other application
    // is able to lock it before this code manages to open the file.
    var stream = File.OpenRead(e.FullPath);
}

免责声明:CustomWatcher可能需要具有IDisposable并适当地处理FileSystemWatcher。该代码仅显示了如何实现所需功能的示例。