示例#1
0
    /// <summary>
    /// Sets up watchers for the type given before performing a File.Move operation and checking for
    /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
    /// we ensure that it is not observed.
    ///
    /// This test will use the NotifyFilter attribute of the FileSystemWatcher before the move and subsequent
    /// checks are made.
    /// </summary>
    private static void MoveAndCheck_WithNotifyFilter(WatcherChangeTypes eventType, bool moveRaisesEvent)
    {
        using (var file = Utility.CreateTestFile(Guid.NewGuid().ToString()))
            using (var watcher = new FileSystemWatcher("."))
            {
                watcher.NotifyFilter = NotifyFilters.FileName;
                watcher.Filter       = Path.GetFileName(file.Path);
                AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, eventType);

                string newName = file.Path + "_" + eventType.ToString();
                Utility.EnsureDelete(newName);

                watcher.EnableRaisingEvents = true;

                file.Move(newName);

                if (moveRaisesEvent)
                {
                    Utility.ExpectEvent(eventOccured, eventType.ToString());
                }
                else
                {
                    Utility.ExpectNoEvent(eventOccured, eventType.ToString());
                }
            }
    }
示例#2
0
        //  This method is called when a file is created, renamed, or deleted.
        private void FileWatcherReplaced(object source, FileSystemEventArgs e)
        {
            WatcherChangeTypes wct = e.ChangeType;

            if (e is RenamedEventArgs)
            {
                Log.Info(InputName + "/" + SelectorName + " FileWatcherReplaced: " + wct.ToString() + " " + ((RenamedEventArgs)e).OldFullPath + " to " + e.FullPath);
            }
            else
            {
                Log.Info(InputName + "/" + SelectorName + " FileWatcherReplaced: " + wct.ToString() + ", " + e.FullPath);
            }

            // close old file
            if (reader != null)
            {
                reader.Close();
                reader  = null;
                errtime = 0;
            }

            // signal log file reader thread
            if (!onlyWatcher)
            {
                wait.Set();
            }
            else
            {
                // FileSystemWatcher process events in sequence so we
                // don't have to synchronize ProcessLines call here
                exit.Reset();
                ProcessLines();
                exit.Set();
            }
        }
示例#3
0
    /// <summary>
    /// Sets up watchers for the type given before performing a File.Move operation and checking for
    /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
    /// we ensure that it is not observed.
    ///
    /// This test checks for when the file being moved has a destination directory that is outside of
    /// the path of the FileSystemWatcher.
    /// </summary>
    private static void MoveAndCheck_DifferentDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent)
    {
        using (var dir = Utility.CreateTestDirectory(Guid.NewGuid().ToString()))
            using (var dir_unwatched = new TemporaryTestDirectory(Path.GetRandomFileName()))
                using (var watcher = new FileSystemWatcher())
                {
                    // put everything in our own directory to avoid collisions
                    watcher.Path   = Path.GetFullPath(dir.Path);
                    watcher.Filter = "*.*";

                    // create a file
                    using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file")))
                    {
                        watcher.EnableRaisingEvents = true;
                        AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, eventType);

                        // Move the testFile to a different name in the same directory
                        testFile.Move(Path.Combine(dir_unwatched.Path, testFile.Name + "_" + eventType.ToString()));

                        // Test which events are thrown
                        if (moveRaisesEvent)
                        {
                            Utility.ExpectEvent(eventOccured, eventType.ToString());
                        }
                        else
                        {
                            Utility.ExpectNoEvent(eventOccured, eventType.ToString());
                        }
                    }
                }
    }
        /// <summary>
        /// Sets up watchers for the type given before performing a File.Move operation and checking for
        /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
        /// we ensure that it is not observed.
        ///
        /// This test checks for when the file being moved has a destination directory that is outside of
        /// the path of the FileSystemWatcher.
        /// </summary>
        private void MoveAndCheck_DifferentDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent)
        {
            using (var testDirectory = new TempDirectory(GetTestFilePath()))
                using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, GetTestFileName())))
                    using (var dir_unwatched = new TempDirectory(Path.Combine(testDirectory.Path, GetTestFileName())))
                        using (var watcher = new FileSystemWatcher(testDirectory.Path))
                        {
                            // put everything in our own directory to avoid collisions
                            watcher.Path   = Path.GetFullPath(dir.Path);
                            watcher.Filter = "*.*";

                            // create a file
                            using (var testFile = new TempFile(Path.Combine(dir.Path, "file")))
                            {
                                watcher.EnableRaisingEvents = true;
                                AutoResetEvent eventOccurred = WatchForEvents(watcher, eventType);

                                // Move the testFile to a different name in the same directory
                                File.Move(testFile.Path, Path.Combine(dir_unwatched.Path, Path.GetFileName(testFile.Path) + "_" + eventType.ToString()));

                                // Test which events are thrown
                                if (moveRaisesEvent)
                                {
                                    ExpectEvent(eventOccurred, eventType.ToString());
                                }
                                else
                                {
                                    ExpectNoEvent(eventOccurred, eventType.ToString());
                                }
                            }
                        }
        }
        /// <summary>
        /// Sets up watchers for the type given before performing a File.Move operation and checking for
        /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
        /// we ensure that it is not observed.
        ///
        /// This test will use the NotifyFilter attribute of the FileSystemWatcher before the move and subsequent
        /// checks are made.
        /// </summary>
        private void MoveAndCheck_WithNotifyFilter(WatcherChangeTypes eventType, bool moveRaisesEvent)
        {
            using (var testDirectory = new TempDirectory(GetTestFilePath()))
                using (var file = new TempFile(Path.Combine(testDirectory.Path, GetTestFileName())))
                    using (var watcher = new FileSystemWatcher(testDirectory.Path))
                    {
                        watcher.NotifyFilter = NotifyFilters.FileName;
                        watcher.Filter       = Path.GetFileName(file.Path);
                        AutoResetEvent eventOccurred = WatchForEvents(watcher, eventType);

                        string newName = Path.Combine(testDirectory.Path, GetTestFileName());
                        watcher.EnableRaisingEvents = true;

                        File.Move(file.Path, newName);

                        if (moveRaisesEvent)
                        {
                            ExpectEvent(eventOccurred, eventType.ToString());
                        }
                        else
                        {
                            ExpectNoEvent(eventOccurred, eventType.ToString());
                        }
                    }
        }
    private void OnDeleted(object source, FileSystemEventArgs e)
    {
        //  Show that a file has been created, changed, or deleted.
        WatcherChangeTypes wct = e.ChangeType;

        Console.WriteLine("File {0} {1}", e.FullPath, wct.ToString());
        MessageBox.Show("OnDeleted File {0} {1}" + e.FullPath + wct.ToString());

        tim.Start();
    }
示例#7
0
        private void OnRenamed(object sender, RenamedEventArgs e)
        {
            WatcherChangeTypes wct = e.ChangeType;

            log += " Renamed " + e.OldFullPath + " " + e.FullPath + " " + wct.ToString() + DateTime.Now + "\n";
            logTextbox.Invoke(new Action(() => logTextbox.Text = log));
        }
        private static void AlCambiar(object source, FileSystemEventArgs e)
        {
            WatcherChangeTypes TipoDeCambio = e.ChangeType;
            string             tc           = TipoDeCambio.ToString();

            //MANDA IMPRESION A IMPRESORA POR DEFECTO

            if (tc == "Created")
            {
                try
                {
                    Process p = new Process();
                    p.StartInfo = new ProcessStartInfo()
                    {
                        CreateNoWindow = true,
                        Verb           = "print",
                        FileName       = e.FullPath
                    };
                    p.Start();
                    p.WaitForExit(1000);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Factura" + e.Name + ex.Message);
                }
            }
        }
示例#9
0
 private static void WriteToLog(string logFilePath, string name, WatcherChangeTypes change)
 {
     using (StreamWriter sw = new StreamWriter(logFilePath, true))
     {
         sw.WriteLine($"{DateTime.Now.ToString()}\t{name}\t{change.ToString().ToUpper()}");
     }
 }
示例#10
0
    void OnChanged(System.Object source, System.IO.FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
        WatcherChangeTypes wct = e.ChangeType;

        Debug.Log(e.FullPath + wct.ToString());
    }
示例#11
0
        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            bool isDecoy = false;

            foreach (var fileName in decoyFiles)
            {
                if (fileName == e.OldName)
                {
                    isDecoy = true;
                    break;
                }
            }

            WatcherChangeTypes wct  = e.ChangeType;
            string             time = DateTime.Now.ToString("HH:mm:ss.fff");

            Console.Write("{0} ", time);

            if (isDecoy)
            {
                Console.Write("DECOY ", Color.Red);
            }

            Console.Write(wct.ToString().ToUpper(), Color.Cyan);
            Console.Write(" FROM ", Color.Yellow);
            Console.Write("{0}", e.OldFullPath);
            Console.Write(" TO ", e.FullPath, Color.Yellow);
            Console.WriteLine("{0}", e.FullPath);
        }
示例#12
0
        WaitForChangedResult waitForChangedHandler(string directoryPath, string filter,
                                                   WatcherChangeTypes watcherChangeTypes, int timeOut)
        {
            if (watcherChangeTypes != WatcherChangeTypes.Created)
            {
                throw new NotImplementedException(watcherChangeTypes.ToString());
            }
            var filesList = new ArrayList(Directory.GetFiles(directoryPath, filter));

            var waitForChangedResult = new WaitForChangedResult();

            while (true)
            {
                var newFilesList = new ArrayList(Directory.GetFiles(directoryPath, filter));
                foreach (string file in newFilesList)
                {
                    if (!filesList.Contains(file))
                    {
                        waitForChangedResult.ChangeType = WatcherChangeTypes.Created;
                        waitForChangedResult.Name       = file;
                        return(waitForChangedResult);
                    }
                }
            }
        }
示例#13
0
        // This method is called when a file is renamed.
        // 파일 이름 변경시
        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            //  Show that a file has been renamed.
            WatcherChangeTypes wct = e.ChangeType;

            Console.WriteLine("File {0}을 {1}로 {2} 했습니다.", e.OldFullPath, e.FullPath, wct.ToString());
        }
示例#14
0
        //Enumerable
        //string str;
        //int a = 1;
        //double number;
        //float num;
        //bool flag;
        //DateTime dataTime;
        //Enum eEnum;
        //byte bytes;
        //long numone;
        //short numtwo;
        //sbyte bSbyte;
        //ulong onUlong;
        //decimal dDecimal;
        //char dchar;
        //object obj = null;
        //var result = obj is null;
        //var f = obj as object;
        #endregion

        #region File 类
        //if (File.Exists("D:/2.txt"))
        //{
        //   FileStream fileStream = File.Create("D:/2.txt");

        //   fileStream.Lock(0,fileStream.Length);
        //   StreamWriter sw = new StreamWriter(fileStream);
        //   sw.WriteLine("我是写入\n的字符串");
        //   fileStream.Unlock(0,fileStream.Length);
        //   sw.Flush();
        //}
        #endregion

        #region FileInfo 类

        ////读取文件
        //FileInfo fileInfo = new FileInfo("D:/10.txt");
        ////文件不存在则创建
        //if (!fileInfo.Exists)
        //{
        //    using (fileInfo.Create()) { }
        //}

        //    //写入文件内容
        //    using (StreamWriter streamWriter = fileInfo.AppendText())
        //    {
        //        //要写入的字符串
        //        streamWriter.Write("我是写入的字符串");
        //        //写入流
        //        streamWriter.Flush();
        //    }
        //    //读取文件内容
        //    StreamReader streamReader = fileInfo.OpenText();
        //    //关闭流
        //    //using 和 Close效果等同 using用完资源之后立马释放
        //    //streamReader.Close();
        //    using (streamReader)
        //    {
        //        //输出文件内容
        //        Console.WriteLine(streamReader.ReadToEnd());
        //        //streamWriter.Close();
        //    }
        #endregion

        #region 转换
        //string 转换成 Char[]
        //string ss = "abcdefg";
        //char[] cc = ss.ToCharArray();
        //Char[] 转换成string

        //string s = new string(cc);
        //byte[] 与 string 之间的转换

        //byte[] bb = Encoding.UTF8.GetBytes(ss);
        //string s = Encoding.UTF8.GetString(bb);
        //byte[] 与 char[] 之间的转换
        //byte[] bb;
        //char[] cc = Encoding.ASCII.GetChars(bb);
        //byte[] bb = Encoding.ASCII.GetBytes(cc);
        #endregion

        #region BinaryWriter 类

        //string path = "D:/10.txt";
        //if (!File.Exists(path))
        //{
        //    using (File.Create(path)) { }

        //}
        //FileStream fs =  File.Open("D:/10.txt", FileMode.Open);
        //BinaryReader br = new BinaryReader(fs,Encoding.Default);
        //char [] cr =  br.ReadChars(int.Parse(fs.Length.ToString()));
        //string crstr = new string(cr);
        //Console.WriteLine(crstr);
        //BinaryWriter bw = new BinaryWriter(fs,Encoding.Default);
        //string bstr = "我是刚刚写入的";
        ////Encoding.UTF8.GetBytes()
        ////bw.Write();
        //Console.ReadKey();

        #endregion

        #region DriveInfo 类

        //DriveInfo[] allDrives = DriveInfo.GetDrives();

        //foreach (DriveInfo d in allDrives)
        //{
        //    Console.WriteLine("Drive {0}", d.Name);
        //    Console.WriteLine("  Drive type: {0}", d.DriveType);
        //    if (d.IsReady)
        //    {
        //        Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
        //        Console.WriteLine("  File system: {0}", d.DriveFormat);
        //        Console.WriteLine(
        //            "  Available space to current user:{0, 15} bytes",
        //            d.AvailableFreeSpace);

        //        Console.WriteLine(
        //            "  Total available space:          {0, 15} bytes",
        //            d.TotalFreeSpace);

        //        Console.WriteLine(
        //            "  Total size of drive:            {0, 15} bytes ",
        //            d.TotalSize);
        //    }
        //}
        #endregion

        #region FileSystemEventArgs 类
        //  Create a FileSystemWatcher to monitor all files on drive C.
        //FileSystemWatcher fsw = new FileSystemWatcher("D:\\")
        //{
        //    NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
        //                   | NotifyFilters.FileName | NotifyFilters.DirectoryName
        //};

        ////  Watch for changes in LastAccess and LastWrite times, and
        ////  the renaming of files or directories.

        ////  Register a handler that gets called when a
        ////  file is created, changed, or deleted.
        //fsw.Changed += OnChanged;

        //fsw.Created += OnChanged;

        //fsw.Deleted += OnChanged;

        ////  Register a handler that gets called when a file is renamed.
        //fsw.Renamed += OnRenamed;

        ////  Register a handler that gets called if the
        ////  FileSystemWatcher needs to report an error.
        //fsw.Error += OnError;

        ////  Begin watching.
        //fsw.EnableRaisingEvents = true;

        //Console.WriteLine("Press \'Enter\' to quit the sample.");
        //Console.ReadLine();

        #endregion

        #region MyRegion


        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            //  Show that a file has been created, changed, or deleted.
            WatcherChangeTypes wct = e.ChangeType;

            Console.WriteLine("File {0} {1}", e.FullPath, wct.ToString());
        }
示例#15
0
        private static void Alcambiar(object source, FileSystemEventArgs e)
        {
            WatcherChangeTypes TipoCambio = e.ChangeType;

            Console.WriteLine("El Archivo {0} tuvo un cambio de {1}",

                              e.FullPath, TipoCambio.ToString());



            NotifyIcon nb1 = new NotifyIcon();

            nb1.Icon = SystemIcons.Application;
            nb1.Icon = new Icon(@"C:\Temporal\launch_122853.ico");


            NotifyIcon tray = new NotifyIcon();

            tray.Icon           = nb1.Icon;
            tray.Text           = "INFORMACIÓN";
            tray.BalloonTipText = "Se ha detectado un cambio en archivo DATA-TEC";;

            tray.Visible = true;
            tray.ShowBalloonTip(10);

            System.Diagnostics.ProcessStartInfo start =
                new System.Diagnostics.ProcessStartInfo();
            //start.FileName = dir + @"\Myprocesstostart.exe";
            start.WindowStyle = ProcessWindowStyle.Hidden;
        }
示例#16
0
        private void OnRenamed(object source, RenamedEventArgs e)
        {
            //  Show that a file has been renamed.
            WatcherChangeTypes wct = e.ChangeType;

            slLogger.WriteLogLine(wct.ToString() + " Old Path: " + e.OldFullPath + " New Path: " + e.FullPath);
        }
示例#17
0
        // Starts a timer when something changes
        private void OnFileActivity(object o, FileSystemEventArgs fse_args)
        {
            if (fse_args.Name.StartsWith(".git/"))
            {
                return;
            }

            WatcherChangeTypes wct = fse_args.ChangeType;

            if (AnyDifferences)
            {
                _IsBuffering = true;

                // Only fire the event if the timer has been stopped.
                // This prevents multiple events from being raised whilst "buffering".
                if (!HasChanged)
                {
                    SparkleEventArgs args = new SparkleEventArgs("ChangesDetected");

                    if (ChangesDetected != null)
                    {
                        ChangesDetected(this, args);
                    }
                }

                SparkleHelpers.DebugInfo("Event", "[" + Name + "] " + wct.ToString() + " '" + fse_args.Name + "'");
                SparkleHelpers.DebugInfo("Local", "[" + Name + "] Changes found, checking if settled.");

                RemoteTimer.Stop();

                lock (ChangeLock) {
                    HasChanged = true;
                }
            }
        }
示例#18
0
        public void OnChanged(object source, FileSystemEventArgs dave)
        {
            WatcherChangeTypes wct      = dave.ChangeType;
            string             fullpath = dave.FullPath;
            string             m        = fullpath + " " + wct.ToString();

            c.Add(m);
        }
    private void OnCreated(object source, FileSystemEventArgs e)
    {
        //  Show that a file has been created
        WatcherChangeTypes wct = e.ChangeType;

        MessageBox.Show("OnCreated File " + e.FullPath + wct.ToString());
        tim.Start();        // start timer as you get file.exe found....
    }
    //  This method is called when a file is renamed.
    private void OnRenamed(object source, RenamedEventArgs e)
    {
        //  Show that a file has been renamed.
        WatcherChangeTypes wct = e.ChangeType;

        MessageBox.Show("OnRenamed File " + e.OldFullPath + e.FullPath + wct.ToString());
        tim.Start();         // start timer as you get file.exe found....
    }
示例#21
0
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            WatcherChangeTypes wct      = e.ChangeType;
            string             FullPath = e.FullPath;
            string             Name     = e.Name;

            slLogger.WriteLogLine(wct.ToString() + " Path: " + FullPath);
        }
示例#22
0
        private void OnRenamed(object source, FileSystemEventArgs e)
        {
            FWLogger.Log.Debug("OnChanged");
            //Show that a file has been created, changed, or deleted.
            WatcherChangeTypes wct = e.ChangeType;

            FWLogger.Log.InfoFormat("File {0} {1}", e.FullPath, wct.ToString());
        }
示例#23
0
        //  This method is called when a file is renamed.
        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            //  Show that a file has been renamed.
            WatcherChangeTypes wct        = e.ChangeType;
            FileSystemWatcher  ParentFSW  = source as FileSystemWatcher;
            frmMain            ParentForm = ParentFSW.SynchronizingObject as frmMain;

            ParentForm.MyText = "File {0} {2} to {1}" + e.OldFullPath + e.FullPath + wct.ToString();
        }
 /// <summary>
 /// Sets up watchers for the type given before performing a File.Move operation and checking for
 /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
 /// we ensure that it is not observed.
 ///
 /// This test will move the source file of a file within a nested directory
 /// </summary>
 private void MoveAndCheck_NestedDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent)
 {
     TestNestedDirectoriesHelper(GetTestFilePath(), eventType, (AutoResetEvent eventOccurred, TempDirectory ttd) =>
     {
         using (var nestedFile = new TempFile(Path.Combine(ttd.Path, "nestedFile" + eventType.ToString())))
         {
             File.Move(nestedFile.Path, nestedFile.Path + "_2");
             if (moveRaisesEvent)
             {
                 ExpectEvent(eventOccurred, eventType.ToString());
             }
             else
             {
                 ExpectNoEvent(eventOccurred, eventType.ToString());
             }
         }
     });
 }
示例#25
0
        public void OnRenamed(object source, RenamedEventArgs rick)
        {
            WatcherChangeTypes wct      = rick.ChangeType;
            string             fullpath = rick.FullPath;
            string             oldpath  = rick.OldFullPath;
            string             m        = oldpath + " " + fullpath + " " + wct.ToString();

            c.Add(m);
        }
示例#26
0
 /// <summary>
 /// Sets up watchers for the type given before performing a File.Move operation and checking for
 /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
 /// we ensure that it is not observed.
 ///
 /// This test will move the source file of a file within a nested directory
 /// </summary>
 private static void MoveAndCheck_NestedDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent)
 {
     Utility.TestNestedDirectoriesHelper(eventType, (AutoResetEvent eventOccured, TemporaryTestDirectory ttd) =>
     {
         using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile" + eventType.ToString())))
         {
             nestedFile.Move(nestedFile.Path + "_2");
             if (moveRaisesEvent)
             {
                 Utility.ExpectEvent(eventOccured, eventType.ToString());
             }
             else
             {
                 Utility.ExpectNoEvent(eventOccured, eventType.ToString());
             }
         }
     });
 }
示例#27
0
 // Watcher noticed changed file
 private void OnWatcherChangedFile(object sender, FileSystemEventArgs e)
 {
     if (!Directory.Exists(e.FullPath))
     {
         WatcherChangeTypes wct             = e.ChangeType;
         string             watcher_message = "File " + e.FullPath + " - " + wct.ToString();
         verifiedSend(watcher_message);
     }
 }
示例#28
0
        private void FileWatcherChanged(object source, FileSystemEventArgs e)
        {
            WatcherChangeTypes wct = e.ChangeType;

            Log.Info(GetType() + "[" + Name
                     + "]: FileWatcherChanged for \"" + filename
                     + "\": " + wct.ToString() + ", " + e.FullPath);

            UpdateConfiguration();
        }
示例#29
0
        //This method is called when a file is created, changed, or deleted.
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            //Show that a file has been created, changed, or deleted.
            WatcherChangeTypes wct = e.ChangeType;

            if (WatcherChangeTypes.Changed == e.ChangeType)
            {
                FWLogger.Log.InfoFormat("File {0} {1}", e.FullPath, wct.ToString());
                m_NewFileList.Add(e.FullPath);
            }
        }
示例#30
0
        private void OnDeleted(object source, FileSystemEventArgs e)
        {
            WatcherChangeTypes wct      = e.ChangeType;
            string             FullPath = e.FullPath;
            string             Name     = e.Name;

            RestoreDeletedFile(wct, FullPath, Name);
            slLogger.WriteLogLine(wct.ToString() + " Path: " + e.FullPath);

            //snapshotFactory.BackupFromShadow(e.FullPath);
        }
示例#31
0
        WaitForChangedResult waitForChangedHandler(string directoryPath, string filter,
                                                   WatcherChangeTypes watcherChangeTypes, int timeOut) {
            if (watcherChangeTypes != WatcherChangeTypes.Created)
                throw new NotImplementedException(watcherChangeTypes.ToString());
            var filesList = new ArrayList(Directory.GetFiles(directoryPath, filter));

            var waitForChangedResult = new WaitForChangedResult();
            while (true) {
                var newFilesList = new ArrayList(Directory.GetFiles(directoryPath, filter));
                foreach (string file in newFilesList) {
                    if (!filesList.Contains(file)) {
                        waitForChangedResult.ChangeType = WatcherChangeTypes.Created;
                        waitForChangedResult.Name = file;
                        return waitForChangedResult;

                    }
                }
            }
        }
    /// <summary>
    /// Sets up watchers for the type given before performing a File.Move operation and checking for
    /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
    /// we ensure that it is not observed.
    /// 
    /// This test will use the NotifyFilter attribute of the FileSystemWatcher before the move and subsequent
    /// checks are made.
    /// </summary>
    private static void MoveAndCheck_WithNotifyFilter(WatcherChangeTypes eventType, bool moveRaisesEvent)
    {
        using (var file = Utility.CreateTestFile(Guid.NewGuid().ToString()))
        using (var watcher = new FileSystemWatcher("."))
        {
            watcher.NotifyFilter = NotifyFilters.FileName;
            watcher.Filter = Path.GetFileName(file.Path);
            AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, eventType);

            string newName = file.Path + "_" + eventType.ToString();
            Utility.EnsureDelete(newName);

            watcher.EnableRaisingEvents = true;

            file.Move(newName);

            if (moveRaisesEvent)
                Utility.ExpectEvent(eventOccurred, eventType.ToString());
            else
                Utility.ExpectNoEvent(eventOccurred, eventType.ToString());
        }
    }
 /// <summary>
 /// Sets up watchers for the type given before performing a File.Move operation and checking for
 /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
 /// we ensure that it is not observed.
 /// 
 /// This test will move the source file of a file within a nested directory
 /// </summary>
 private static void MoveAndCheck_NestedDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent)
 {
     Utility.TestNestedDirectoriesHelper(eventType, (AutoResetEvent eventOccurred, TemporaryTestDirectory ttd) =>
     {
         using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile" + eventType.ToString())))
         {
             nestedFile.Move(nestedFile.Path + "_2");
             if (moveRaisesEvent)
                 Utility.ExpectEvent(eventOccurred, eventType.ToString());
             else
                 Utility.ExpectNoEvent(eventOccurred, eventType.ToString());
         }
     });
 }
    /// <summary>
    /// Sets up watchers for the type given before performing a File.Move operation and checking for
    /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
    /// we ensure that it is not observed.
    /// 
    /// This test checks for when the file being moved has a destination directory that is outside of
    /// the path of the FileSystemWatcher.
    /// </summary>
    private static void MoveAndCheck_DifferentDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent)
    {
        using (var dir = Utility.CreateTestDirectory(Guid.NewGuid().ToString()))
        using (var dir_unwatched = new TemporaryTestDirectory(Path.GetRandomFileName()))
        using (var watcher = new FileSystemWatcher())
        {
            // put everything in our own directory to avoid collisions
            watcher.Path = Path.GetFullPath(dir.Path);
            watcher.Filter = "*.*";

            // create a file
            using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file")))
            {
                watcher.EnableRaisingEvents = true;
                AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, eventType);

                // Move the testFile to a different name in the same directory
                testFile.Move(Path.Combine(dir_unwatched.Path, testFile.Name + "_" + eventType.ToString()));

                // Test which events are thrown
                if (moveRaisesEvent)
                    Utility.ExpectEvent(eventOccurred, eventType.ToString());
                else
                    Utility.ExpectNoEvent(eventOccurred, eventType.ToString());
            }
        }
    }
        private void FileMove_DifferentWatchedDirectory(WatcherChangeTypes eventType)
        {
            using (var testDirectory = new TempDirectory(GetTestFilePath()))
            using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir")))
            using (var dir_adjacent = new TempDirectory(Path.Combine(testDirectory.Path, "dir_adj")))
            using (var testFile = new TempFile(Path.Combine(dir.Path, "file")))
            using (var watcher = new FileSystemWatcher(testDirectory.Path, "*"))
            {
                string sourcePath = testFile.Path;
                string targetPath = Path.Combine(dir_adjacent.Path, Path.GetFileName(testFile.Path) + "_" + eventType.ToString());

                // Move the testFile to a different directory under the Watcher
                Action action = () => File.Move(sourcePath, targetPath);
                Action cleanup = () => File.Move(targetPath, sourcePath);

                ExpectEvent(watcher, eventType, action, cleanup, new string[] { dir.Path, dir_adjacent.Path });
            }
        }
示例#36
0
        /// <summary>
        /// Sets up watchers for the type given before performing a File.Move operation and checking for
        /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
        /// we ensure that it is not observed.
        /// 
        /// This test will use the NotifyFilter attribute of the FileSystemWatcher before the move and subsequent
        /// checks are made.
        /// </summary>
        private void MoveAndCheck_WithNotifyFilter(WatcherChangeTypes eventType, bool moveRaisesEvent)
        {
            using (var testDirectory = new TempDirectory(GetTestFilePath()))
            using (var file = new TempFile(Path.Combine(testDirectory.Path, GetTestFileName())))
            using (var watcher = new FileSystemWatcher(testDirectory.Path))
            {
                watcher.NotifyFilter = NotifyFilters.FileName;
                watcher.Filter = Path.GetFileName(file.Path);
                AutoResetEvent eventOccurred = WatchForEvents(watcher, eventType);

                string newName = Path.Combine(testDirectory.Path, GetTestFileName());
                watcher.EnableRaisingEvents = true;

                File.Move(file.Path, newName);

                if (moveRaisesEvent)
                    ExpectEvent(eventOccurred, eventType.ToString());
                else
                    ExpectNoEvent(eventOccurred, eventType.ToString());
            }
        }
示例#37
0
 /// <summary>
 /// Sets up watchers for the type given before performing a File.Move operation and checking for
 /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
 /// we ensure that it is not observed.
 /// 
 /// This test will move the source file of a file within a nested directory
 /// </summary>
 private void MoveAndCheck_NestedDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent)
 {
     TestNestedDirectoriesHelper(GetTestFilePath(), eventType, (AutoResetEvent eventOccurred, TempDirectory ttd) =>
     {
         using (var nestedFile = new TempFile(Path.Combine(ttd.Path, "nestedFile" + eventType.ToString())))
         {
             File.Move(nestedFile.Path, nestedFile.Path + "_2");
             if (moveRaisesEvent)
                 ExpectEvent(eventOccurred, eventType.ToString());
             else
                 ExpectNoEvent(eventOccurred, eventType.ToString());
         }
     });
 }
示例#38
0
        /// <summary>
        /// Sets up watchers for the type given before performing a File.Move operation and checking for
        /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
        /// we ensure that it is not observed.
        /// 
        /// This test will move the source file to a destination file in the same directory i.e. rename it
        /// </summary>
        private void MoveAndCheck_SameDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent)
        {
            using (var testDirectory = new TempDirectory(GetTestFilePath()))
            using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, GetTestFileName())))
            using (var watcher = new FileSystemWatcher(testDirectory.Path))
            {
                // put everything in our own directory to avoid collisions
                watcher.Path = Path.GetFullPath(dir.Path);
                watcher.Filter = "*.*";

                // create a file
                using (var testFile = new TempFile(Path.Combine(dir.Path, "file")))
                {
                    watcher.EnableRaisingEvents = true;
                    AutoResetEvent eventOccurred = WatchForEvents(watcher, eventType);

                    // Move the testFile to a different name in the same directory
                    File.Move(testFile.Path, testFile.Path + "_" + eventType.ToString());

                    // Test that the event is observed or not observed
                    if (moveRaisesEvent)
                        ExpectEvent(eventOccurred, eventType.ToString());
                    else
                        ExpectNoEvent(eventOccurred, eventType.ToString());
                }
            }
        }
        private void FileMove_NestedDirectory(WatcherChangeTypes eventType, bool includeSubdirectories)
        {
            using (var dir = new TempDirectory(GetTestFilePath()))
            using (var firstDir = new TempDirectory(Path.Combine(dir.Path, "dir1")))
            using (var nestedDir = new TempDirectory(Path.Combine(firstDir.Path, "nested")))
            using (var nestedFile = new TempFile(Path.Combine(nestedDir.Path, "nestedFile" + eventType.ToString())))
            using (var watcher = new FileSystemWatcher(dir.Path, "*"))
            {
                watcher.NotifyFilter = NotifyFilters.FileName;
                watcher.IncludeSubdirectories = includeSubdirectories;

                string sourcePath = nestedFile.Path;
                string targetPath = nestedFile.Path + "_2";

                // Move the testFile to a different name within the same nested directory
                Action action = () => File.Move(sourcePath, targetPath);
                Action cleanup = () => File.Move(targetPath, sourcePath);

                if ((eventType & WatcherChangeTypes.Deleted) > 0)
                    ExpectEvent(watcher, eventType, action, cleanup, new string[] { targetPath, sourcePath });
                else
                    ExpectEvent(watcher, eventType, action, cleanup, targetPath);
            }
        }
        private void FileMove_SameDirectory(WatcherChangeTypes eventType)
        {
            using (var testDirectory = new TempDirectory(GetTestFilePath()))
            using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir")))
            using (var testFile = new TempFile(Path.Combine(dir.Path, "file")))
            using (var watcher = new FileSystemWatcher(dir.Path, "*"))
            {
                string sourcePath = testFile.Path;
                string targetPath = testFile.Path + "_" + eventType.ToString();

                // Move the testFile to a different name in the same directory
                Action action = () => File.Move(sourcePath, targetPath);
                Action cleanup = () => File.Move(targetPath, sourcePath);

                if ((eventType & WatcherChangeTypes.Deleted) > 0)
                    ExpectEvent(watcher, eventType, action, cleanup, new string[] { sourcePath, targetPath });
                else
                    ExpectEvent(watcher, eventType, action, cleanup, targetPath);
            }
        }