Beispiel #1
0
        static void Main(string[] args)
        {
            var consoleTracer = new ConsoleTraceListener(true);

            Trace.Listeners.Add(consoleTracer);
            consoleTracer.Name = "ManualFileSystemWatcherTrace";
            var dir = @"c:\test\manual";

            if (args.Count() > 0)
            {
                dir = args[0];
            }

            Console.WriteLine($"Scan directory: {dir}");
            //SimpleTestCreateNewFile s = new SimpleTestCreateNewFile(); s.TestNewFiles(); return;

            ManualScanPhysicalFileSystemWatcherSettings settings =
                new ManualScanPhysicalFileSystemWatcherSettings {
                ScanDirectory = dir, ScanSpeedInSeconds = 1
            };
            ManualScanPhysicalFileSystemWatcher w = new ManualScanPhysicalFileSystemWatcher(settings);

            w.Open(new FilterAndCallbackArgument(String.Empty, CallbackChanges));

            // Uncomment for Physical file watcher - for testing purposes only
            //PhysicalFileSystemWatcherWrapperSettings settingsP =
            //    new PhysicalFileSystemWatcherWrapperSettings { ScanDirectory = dir };
            //PhysicalFileSystemWatcherWrapper wP = new PhysicalFileSystemWatcherWrapper(settingsP);
            //wP.Open(new FilterAndCallbackArgument(String.Empty, CallbackChanges));


            Console.WriteLine("Waiting for file changes\r\n\r\n");
            Console.ReadLine();
        }
Beispiel #2
0
        public void TestNewFiles()
        {
            var scanDirectory = GetAndPrepareTempDirectory();
            var m             = new ManualScanPhysicalFileSystemWatcher(new ManualScanPhysicalFileSystemWatcherSettings {
                ScanDirectory = scanDirectory, ScanSpeedInSeconds = 1
            });

            ManualResetEvent changeDetected  = new ManualResetEvent(false);
            ManualResetEvent scanInitialized = new ManualResetEvent(false);

            m.Open(new FilterAndCallbackArgument(String.Empty, (object o, WatcherCallbackArgs args) =>
            {
                //if (args.ChangeType == IFileSystemWatcherChangeType.Created)
                changeDetected.Set();
            },
                                                 (object o) =>
            {
                scanInitialized.Set();
            }));

            var completedScan = scanInitialized.WaitOne(5000);

            Assert.True(completedScan);

            var file      = File.Create(TempFileName(scanDirectory, "NewFile1.txt"));
            var completed = changeDetected.WaitOne(5000);

            Assert.True(completed);
            file.Dispose();

            m.Dispose();

            DeleteAndReturnTempDirectoryName();
        }
        void CreateFileHelper(Func <string, FileStream> createFileBefore, Func <string, FileStream> createFileAfter, IFileSystemWatcherChangeType type)
        {
            var scanDirectory = GetAndPrepareTempDirectory();

            FileStream fileBefore = createFileBefore == null ? null : createFileBefore(scanDirectory);


            var m = new ManualScanPhysicalFileSystemWatcher(new ManualScanPhysicalFileSystemWatcherSettings {
                ScanDirectory = scanDirectory, ScanSpeedInSeconds = 0
            });

            ManualResetEvent changeDetected  = new ManualResetEvent(false);
            ManualResetEvent scanInitialized = new ManualResetEvent(false);

            m.Open(new FilterAndCallbackArgument(String.Empty, (object o, WatcherCallbackArgs args) =>
            {
                if (args.ChangeType == type)
                {
                    changeDetected.Set();
                }
            },
                                                 (object o) =>
            {
                scanInitialized.Set();
            }));

            var completedScan = scanInitialized.WaitOne(500);

            Assert.True(completedScan);

            FileStream file      = createFileAfter == null ? null : createFileAfter(scanDirectory);
            var        completed = changeDetected.WaitOne(500);

            Assert.True(completed);
            file?.Dispose();
            fileBefore?.Dispose();

            m.Dispose();

            DeleteAndReturnTempDirectoryName();
        }