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(...);
...
}