部分代码
public void OnRenamed(object source, RenamedEventArgs e)
      {
         try
         {
            StreamWriter sw = new StreamWriter("D:/Log.txt", true);
            StreamWriter sw1 = new StreamWriter("D:/Log1.txt", true);
            StreamWriter sw2 = new StreamWriter("D:/Log2.txt", true);
            sw.WriteLine("File renamed from {0} to {1}", e.OldName, e.FullPath);
            sw1.WriteLine("File renamed from {0} to {1}", e.OldName, e.FullPath);
            sw2.WriteLine("File renamed from {0} to {1}", e.OldName, e.FullPath);
            sw.Close();
            sw1.Close();
            sw2.Close();
            this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),"Wrote renamed event to log");
            this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText1), "Wrote renamed event to log");
            this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText2), "Wrote renamed event to log");
         }
         catch (IOException)
         {
            this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),"Error Writing to log");
            this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText1), "Error Writing to log");
            this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText2), "Error Writing to log");
         }
      }
…………上述是文件若变化时的代码
 private void cmdWatch_Click(object sender, EventArgs e)//watch键
      {
         watcher.Path = Path.GetDirectoryName(txtLocation.Text);
         watcher.Filter = Path.GetFileName(txtLocation.Text);
         watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
         lblWatch.Text = "Watching " + txtLocation.Text;
         // Begin watching.
         watcher.EnableRaisingEvents = true;
      }这一部分是监控键的代码
我现在遇到的问题是我定义了3个watcher,想让每个watcher监控的文件的变化结果分别进入对应的txt文件,但是现在的结果是每个txt里面均有所有的变化,不知道如何解决,请各路大神帮帮忙……另外想问一下是不是当上述第一部分函数在运用时就不可以再运用了?

解决方案 »

  1.   

    你好像都是用的是txtLocation.Text,是不是指向了同一个文件
      

  2.   

    不是的,不同的监控键对应不同的text,如下 
     private void button2_Click(object sender, EventArgs e)//第二个监控键
          {
              watcher1.Path = Path.GetDirectoryName(textBox1.Text);
              watcher1.Filter = Path.GetFileName(textBox1.Text);
              watcher1.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
              label1.Text = "Watching " + textBox1.Text;
              // Begin watching.
              watcher1.EnableRaisingEvents = true;
          }