我如何使用System.IO.FileSystem.Watcher.Polling?

时间:2016-12-09 16:14:49

标签: c# .net-core

我从this link下载了 System.IO.FileSystem.Watcher.Polling

此库来自corefxlab github

有人能告诉我这个图书馆如何运作的样本吗?

我安装了它但是在设置构造函数参数并调用start()之后我没有找到任何事件或者...... !!!

任何人都可以帮我使用吗?!

 PollingWatcher p = new PollingWatcher(@"...path...", true, 10);
 p.Start();
 ??????? // How trace activities ?!?!

1 个答案:

答案 0 :(得分:2)

[Fact]
public static void FileSystemWatcher_Created_File()
{
    var currentDir = Directory.GetCurrentDirectory();
    string fileName = Guid.NewGuid().ToString();

    var watcher = new PollingWatcher(currentDir, false, 100);
    watcher.ChangedDetailed += (changes) =>
    {
        Assert.Equal(1, changes.Length);
        var change = changes[0];
        Assert.Equal(ChangeType.Created, change.ChangeType);
        Assert.Equal(fileName, change.Name);
        Assert.Equal(currentDir, change.Directory);
    };
    watcher.Start();
    Thread.Sleep(200);
    using (var file = new TemporaryTestFile(fileName))
    {
        Thread.Sleep(200);
        watcher.Dispose();
        Thread.Sleep(200);
    }
}

[Fact]
public static void FileSystemWatcher_Deleted_File() 
{
    var currentDir = Directory.GetCurrentDirectory();
    string fileName = Guid.NewGuid().ToString();

    var watcher = new PollingWatcher(currentDir, false, 100);

    using (var file = new TemporaryTestFile(fileName))
    {
        watcher.ChangedDetailed += (changes) =>
        {
            Assert.Equal(1, changes.Length);
            var change = changes[0];
            Assert.Equal((byte)ChangeType.Deleted, (byte)change.ChangeType);
            Assert.Equal(fileName, change.Name);
            Assert.Equal(currentDir, change.Directory);
        };
        Thread.Sleep(100);
        watcher.Start();
        Thread.Sleep(200);
    }

    Thread.Sleep(200);
    watcher.Dispose();
    Thread.Sleep(200);
}

[Fact]
public static void FileSystemWatcher_Changed_File() 
{
    var currentDir = Directory.GetCurrentDirectory();
    string fileName = Guid.NewGuid().ToString();

    var watcher = new PollingWatcher(currentDir, false, 100);

    using (var file = new TemporaryTestFile(fileName))
    {
        watcher.Start();
        Thread.Sleep(200);
        watcher.ChangedDetailed += (changes) =>
        {
            Assert.Equal(1, changes.Length);
            var change = changes[0];
            Assert.Equal(ChangeType.Changed, change.ChangeType);
            Assert.Equal(fileName, change.Name);
            Assert.Equal(currentDir, change.Directory);
        };
        Thread.Sleep(200);
        file.WriteByte(100);
        Thread.Sleep(200);
        watcher.Dispose();
        Thread.Sleep(200);
    }
}

以上代码取自该git repo的单元测试。