FileSystemWatcher无法连续运行

时间:2019-05-04 09:29:13

标签: c#

检查下面的代码。我正在按照this Microsoft doc来构建此函数,该函数将监视文件更改无限循环。但是我注意到的问题是-一旦运行这些代码,它便会跟踪第2-3次文件更改,此后便不再继续跟踪更改。注意:我什至不按q,即使它停止跟踪文件更改也是如此。我在这里做什么错?我如何使其运行无限循环并继续跟踪文件更改。预先感谢

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            string location = textBox3.Text;
            if (location != "" & textBox1.Text != "" & textBox2.Text != "" & textBox3.Text != "" & textBox1.Enabled == false & textBox2.Enabled == false)
            {
                GlobalValues.Store = textBox1.Text;
                GlobalValues.Key = textBox2.Text;
                // Create a new FileSystemWatcher and set its properties.
                using (FileSystemWatcher watcher = new FileSystemWatcher())
                {
                    watcher.Path = location;
                    // Watch for changes in LastAccess and LastWrite times, and
                    // the renaming of files or directories.
                    watcher.NotifyFilter = NotifyFilters.LastAccess
                                         | NotifyFilters.LastWrite
                                         | NotifyFilters.FileName
                                         | NotifyFilters.DirectoryName;

                    // Only watch text files.
                    watcher.Filter = "*";
                    // Add event handlers.
                    watcher.Changed += OnChanged;
                    // Begin watching.
                    watcher.EnableRaisingEvents = true;
                    // Wait for the user to quit the program.
                    //Console.WriteLine("Press 'q' to quit the sample.");
                    while (Console.Read() != 'q');

                }
            }
        }

0 个答案:

没有答案