이 오류를 해결하는 데 어려움이 있습니다. 이전에 작동 했었지만 어쩌면 코드가 약간 길어서 어딘가에서 실수를 한 것일 수도 있습니다.FileSystem Watcher 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.
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);
}
}
여기
는 코드입니다. 그것은 myWatcher처럼 보인다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.**
}
가 null의 다음 컨트롤 정지하려고 때 오류가납니다. 하지만이 null이 시작 컨트롤에 할당되었습니다.
올바른, 범인을 얻었다. 감사합니다 백만 – user726720