FileSystem Watcher Object引用未设置为对象的实例

时间:2014-09-25 13:38:21

标签: c# .net winforms filesystemwatcher

我很难排除此错误。它以前工作过,但也许我在某处犯了一个错误,因为代码有点长。

以下是代码:

public class MyFileWatcher
{
    private TextBox _textBox;
    private ListBox _listBox;
    private string _folderDestination;
    FileSystemWatcher  _watcher;
    private int _interval;
    //Timespan created when interval is set
    private TimeSpan _recentTimeSpan;
    Dictionary<string, DateTime> _lastFileEvent = new Dictionary<string, DateTime>();
    DateTime _current;



    public  MyFileWatcher(TextBox textBox, ListBox listBox, string destfolderTextBox ,DateTime current, System.ComponentModel.ISynchronizeInvoke syncObj)
    {
        this._textBox = textBox;
        this._listBox = listBox;
    this._folderDestination = destfolderTextBox;
    this._current = current;

        this._watcher = new FileSystemWatcher();
   this._watcher.SynchronizingObject = syncObj;
        this._watcher.Changed += new FileSystemEventHandler(convertXML);

        this._watcher.IncludeSubdirectories = false;
        this._watcher.Path = textBox.Text;
        this._watcher.EnableRaisingEvents = true;

        // add any other required initialization of the FileSystemWatcher here. 

    }

    public void WatchFile(TextBox ctrlTB, ListBox ctrlLB)
    {
        // FileSystemWatcher _watcher = new FileSystemWatcher();
        //var localTB = ctrlTB as TextBox;
        //var localLB = ctrlLB as ListBox;
        _watcher.Path = ctrlTB.Text;
        _watcher.Path = ctrlTB.Text;



        _watcher.NotifyFilter = NotifyFilters.LastWrite;
        _watcher.Filter = "*.xml";

        _watcher.Changed += new FileSystemEventHandler(convertXML);
      //  _watcher.Error += new ErrorEventHandler(WatcherError);
        // _watcher.Changed += (s, e) => convertXML(s,e); 
        // _watcher.Error += new ErrorEventHandler(WatcherError);

        _watcher.IncludeSubdirectories = false;
        _watcher.EnableRaisingEvents = true;


        ctrlLB.Items.Add("Started Monitoring @ " + ctrlTB.Text);
        ctrlLB.SelectedIndex = ctrlLB.Items.Count - 1;
    }

它由以下控件启动:

 private void button12_Click(object sender, EventArgs e)
    {
        current = new DateTime();

        if ((!Directory.Exists(this.textBox2.Text)) || (!Directory.Exists(this.textBox7.Text)))
        {
            //Form2.ActiveForm.Text = "Please select Source Folder";
            // popup.Show("Please Select Source Folder");
            MessageBox.Show("Please Select Proper Source Folder");
            return;
        }

        else
        {
            textBox2.Enabled = false;

            button12.Enabled = false;
            button11.Enabled = true;
            button2.Enabled = false;
            button7.Enabled = false;
            textBox7.Enabled = false;
            //  button4.Enabled = false;
            // WatchFile();
            string destfolder = textBox7.Text + "\\";
            destfolder += "test.xml";
            MyFileWatcher myWatcher = new MyFileWatcher(textBox2, listBox2, destfolder, current, this);

            myWatcher.WatchFile(textBox2, listBox2);
        }
    }

通常工作正常。但是当尝试通过以下控件停止时出现错误:

    private void button11_Click(object sender, EventArgs e)
    {
        // _watcher.EnableRaisingEvents = false;
        //     _watcher.Changed -= new FileSystemEventHandler(InitList);
        //  _watcher.Dispose();
        //((FileSystemWatcher)sender).Dispose();
        listBox2.Items.Add("Stopped Monitoring Directory ");
        listBox2.SelectedIndex = listBox2.Items.Count - 1;
        textBox2.Enabled = true;

        button10.Enabled = true;
        button2.Enabled = true;
        button7.Enabled = true;
        textBox7.Enabled = true;
       // if (myWatcher != null)
            myWatcher.RemoveWatcher();   **// here is where the error comes up.** 
    }

似乎myWatcher为null。但是为什么这是在启动控件

中分配的null

2 个答案:

答案 0 :(得分:1)

如果此代码编译,则在其他地方声明了另一个myWatcher

您在myWatcher中设置的button12_Click 本地button12_Click

MyFileWatcher myWatcher = new MyFileWatcher(textBox2, listBox2, destfolder, current, this);

这会留下您在myWatcher中访问的其他(全局)button11_Click null:

myWatcher.RemoveWatcher();   **// here is where the error comes up.** 

答案 1 :(得分:1)

button12_Click中,您声明了一个局部变量myWatcher,而不是使用您的类字段(因此,此时有一个已初始化的本地myWatcher并且{{1在类级别保持为null):

myWatcher

这解释了为什么private void button12_Click(object sender, EventArgs e) { ... MyFileWatcher myWatcher = new MyFileWatcher(...); ... } 中的行myWatcher.RemoveWatcher();会抛出异常。

您需要更改button11_Click中的代码才能使用类字段,而不是声明新的局部变量:

button12_Click