Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文件系统监视程序对象引用未设置为对象的实例_C#_.net_Winforms_Filesystemwatcher - Fatal编程技术网

C# 文件系统监视程序对象引用未设置为对象的实例

C# 文件系统监视程序对象引用未设置为对象的实例,c#,.net,winforms,filesystemwatcher,C#,.net,Winforms,Filesystemwatcher,我很难排除这个错误。它以前是工作的,但可能我在某个地方犯了一个错误,因为代码有点长 代码如下: public class MyFileWatcher { private TextBox _textBox; private ListBox _listBox; private string _folderDestination; FileSystemWatcher _watcher; private int _interval; //Timespan

我很难排除这个错误。它以前是工作的,但可能我在某个地方犯了一个错误,因为代码有点长

代码如下:

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是空的。但是为什么这是空的呢?这是在开始控件中分配的

如果这段代码已编译,那么您在别处声明了另一个
myWatcher

您在
按钮12\u点击
中设置的
myWatcher
本地的
按钮12\u点击

MyFileWatcher myWatcher = new MyFileWatcher(textBox2, listBox2, destfolder, current, this);
myWatcher.RemoveWatcher();   **// here is where the error comes up.** 
这将使另一个(全局)
myWatcher
null,您可以在
按钮11\u单击

MyFileWatcher myWatcher = new MyFileWatcher(textBox2, listBox2, destfolder, current, this);
myWatcher.RemoveWatcher();   **// here is where the error comes up.** 

按钮12\u单击
您正在声明一个局部变量
myWatcher
,而不是使用您的类字段(因此,此时有一个已初始化的局部
myWatcher
,类级别的
myWatcher
保持为空):

这就解释了为什么行
myWatcher.RemoveWatcher()按钮11中单击
会引发异常

您需要更改
按钮12中的代码\u单击
以使用类字段,而不是声明新的局部变量:

private void button12_Click(object sender, EventArgs e)
{
    ...
    myWatcher = new MyFileWatcher(...);
    ...
}