public static void FileWatchingThread(object objFileSystemObjectWatcher)
        {
            FileSystemObjectWatcher fileSystemObjectWatcher = (FileSystemObjectWatcher)objFileSystemObjectWatcher;

            FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(fileSystemObjectWatcher.Path);

            fileSystemWatcher.NotifyFilter = NotifyFilters.LastAccess
                                             | NotifyFilters.LastWrite
                                             | NotifyFilters.FileName
                                             | NotifyFilters.DirectoryName;

            fileSystemWatcher.IncludeSubdirectories = fileSystemObjectWatcher.IncludeSubDirectories;
            fileSystemWatcher.Filter = "*";

            fileSystemWatcher.Changed += new FileSystemEventHandler(fileSystemObjectWatcher.OnFileSystemObjectChanged);
            fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemObjectWatcher.OnFileSystemObjectChanged);
            fileSystemWatcher.Deleted += new FileSystemEventHandler(fileSystemObjectWatcher.OnFileSystemObjectChanged);
            fileSystemWatcher.Renamed += new RenamedEventHandler(fileSystemObjectWatcher.OnFileSystemObjectRenamed);

            fileSystemWatcher.EnableRaisingEvents = true;

            Console.WriteLine("{0} : {1} : FileWatchingThread running on \"{2}\" with subdirectories {3}",
                              DateTime.Now,
                              Thread.CurrentThread.ManagedThreadId,
                              fileSystemWatcher.Path,
                              (fileSystemWatcher.IncludeSubdirectories?"included":"not included"));

            fileSystemObjectWatcher._exitEvent.WaitOne();

            Console.WriteLine("{0} : {1} : FileWatchingThread exiting", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
        }
        private static void ProcessingThread(object objFileSystemObjectWatcher)
        {
            FileSystemObjectWatcher fileSystemObjectWatcher = (FileSystemObjectWatcher)objFileSystemObjectWatcher;

            bool fileSystemObjectEventDetected = false;
            bool exitEvent = false;

            while ((!exitEvent) && (fileSystemObjectWatcher.Running))
            {
                WaitHandle[] waitHandles = new WaitHandle[] { fileSystemObjectWatcher._fileSystemObjectWatchEvent, fileSystemObjectWatcher._exitEvent };

                int waitIndex = WaitHandle.WaitAny(waitHandles, fileSystemObjectWatcher._eventResolutionMilliseconds);
                if (waitIndex != System.Threading.WaitHandle.WaitTimeout)
                {
                    // Not a timeout

                    switch (waitIndex)
                    {
                    case 0:     /* fileSystemObjectWatcher._fileSystemObjectWatchEvent */
                        // File System Object Watcher Event occurred
                        Console.WriteLine("{0} : {1} : Detected a File System Object Event", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
                        fileSystemObjectEventDetected = true;
                        break;

                    case 1:     /* fileSystemObjectWatcher._exitEvent */
                        Console.WriteLine("{0} : {1} : Detected an Exit Event", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
                        exitEvent = true;
                        break;

                    default:
                        Console.WriteLine("{0} : {1} : Detected an Unknown Event with WaitIndex {2}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, waitIndex);
                        break;
                    }
                } // Not a timeout
                else
                {
                    // Timeout occurred

                    Console.WriteLine("{0} : {1} : Timeout after {2} ms waiting for File System Object Event",
                                      DateTime.Now,
                                      Thread.CurrentThread.ManagedThreadId,
                                      fileSystemObjectWatcher._eventResolutionMilliseconds);
                    if (fileSystemObjectEventDetected)
                    {
                        // ProcessingThread this File System Object Event
                        fileSystemObjectEventDetected = false;
                        if (fileSystemObjectWatcher._fileSystemObjectWatcherProcessingDelegate != null)
                        {
                            Console.WriteLine("{0} : {1} : Running task", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
                            fileSystemObjectWatcher._fileSystemObjectWatcherProcessingDelegate(
                                fileSystemObjectWatcher);
                        }
                    } // ProcessingThread this File System Object Event
                }     // Timeout occurred
            }         // while

            Console.WriteLine("{0} : {1} : ProcessingThread exiting", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
        }
Пример #3
0
        static void Main(string[] args)
        {
            if (args.Count() > 0)
            {
                string directoryToWatch            = args[0];
                bool   includeSubDirectories       = false;
                int    eventResolutionMilliseconds = 10000;
                if (args.Count() > 1)
                {
                    includeSubDirectories = (args[1].ToLower() == "/s" ? true : false);
                }
                if (args.Count() > 2)
                {
                    try
                    {
                        eventResolutionMilliseconds = System.Convert.ToInt32(args[2]);
                    }
                    catch (Exception)
                    {
                    }
                    Console.WriteLine("Event Resolution is {0} milliseconds", eventResolutionMilliseconds);
                }

                try
                {
                    using (FileSystemObjectWatcher fileSystemObjectWatcher = new FileSystemObjectWatcher(
                               directoryToWatch,
                               includeSubDirectories,
                               ProcessFileSystemEvent,
                               eventResolutionMilliseconds))
                    {
                        // Wait for the user to quit the program

                        fileSystemObjectWatcher.BeginProcessing();

                        Console.WriteLine("Press \'q\' to quit File Watching");
                        int response = 0;
                        do
                        {
                            response = Console.Read();
                        } while (response != 'q');
                    }
                }
                catch (Exception eek)
                {
                    Console.WriteLine("Exception: \"{0}\"", eek.ToString());
                }
            }
        }
Пример #4
0
 private void ProcessFileSystemEvent(FileSystemObjectWatcher fileSystemObjectWatcher)
 {
     Console.WriteLine("{0} : {1} , ProcessFileSystemEvent running", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
     ExecuteBatchFile("fred.bat", fileSystemObjectWatcher);
 }
Пример #5
0
        private void ExecuteBatchFile(string batchFullFilename, FileSystemObjectWatcher fileSystemObjectWatcher)
        {
            string commandInterpreter = Environment.GetEnvironmentVariable("COMSPEC");

            // Pass the following parameters to the batch file
            //  Project Directory
            //  Project Filename
            //  Configuration
            //  Configuration Path
            string batchFileArguments = String.Format("/c \"{0}\" {1}",
                                                      batchFullFilename,
                                                      fileSystemObjectWatcher.Path);

            // Set up a Procees Start Info to control the command execution
            ProcessStartInfo batchFileProcessStartInfo = new ProcessStartInfo(commandInterpreter);

            batchFileProcessStartInfo.UseShellExecute = false;
            batchFileProcessStartInfo.Arguments       = batchFileArguments;
            // batchFileProcessStartInfo.RedirectStandardInput = true;
            batchFileProcessStartInfo.RedirectStandardOutput = true;
            batchFileProcessStartInfo.RedirectStandardError  = true;

            try
            {
                Console.WriteLine("Running \"{0}\" \"{1}\" in \"{2}\"", commandInterpreter, batchFileArguments, Environment.CurrentDirectory);

                Process batchFileProcess = new Process();
                batchFileProcess.StartInfo = batchFileProcessStartInfo;

                batchFileProcess.Start();

                batchFileProcess.WaitForExit();

                Console.WriteLine("Running \"{0}\" produced Exit Code {1}", batchFullFilename, batchFileProcess.ExitCode);

                if (!batchFileProcess.StandardOutput.EndOfStream)
                {
                    Console.WriteLine("Batch File \"{0}\" Standard Output:", batchFullFilename);
                    while (!batchFileProcess.StandardOutput.EndOfStream)
                    {
                        Console.WriteLine("    " + batchFileProcess.StandardOutput.ReadLine());
                    }
                }
                if (!batchFileProcess.StandardError.EndOfStream)
                {
                    Console.WriteLine("Batch File \"{0}\" Standard Error:", batchFullFilename);
                    while (!batchFileProcess.StandardError.EndOfStream)
                    {
                        Console.WriteLine("    " + batchFileProcess.StandardError.ReadLine());
                    }
                }
            }
            catch (Exception eek)
            {
                Console.WriteLine("    *** {0}.{1} : Executing \"{2}\" generated exception \"{3}\"",
                                  MethodBase.GetCurrentMethod().DeclaringType.Name,
                                  MethodBase.GetCurrentMethod().Name,
                                  batchFullFilename,
                                  eek.ToString());
            }
        }