내 FileSystemWatcher가 이벤트를 발생시키지 않습니다. 나는이 유사한 질문을 보았다. 아무도 내 문제에 대한 대답이 아닌 것 같다.FileSystemWatcher 이벤트가 실행되지 않음
* 편집 : 내 목표는 XLS 파일을 디렉토리에 복사하거나 디렉토리에 만들 때를 포착하는 것이다.
- Filesystemwatcher doesn't trigger event
- FileSystemWatcher - event not firing the second time
- FileSystemWatcher Changed event doesn't fire
- FileSystemWatcher - only the change event once firing once?
모니터 클래스 : 목록 상자와
public class Monitor
{
FileSystemWatcher watcher = new FileSystemWatcher();
readonly string bookedPath = @"\\SomeServer\SomeFolder\";
public delegate void FileDroppedEvent(string FullPath);
public event FileDroppedEvent FileDropped;
public delegate void ErrorEvent(Exception ex);
public event ErrorEvent Error;
public Monitor()
{
watcher.Path = bookedPath;
watcher.Filter = "*.xls";
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.Error += new ErrorEventHandler(watcher_Error);
}
void watcher_Error(object sender, ErrorEventArgs e)
{
Error(e.GetException());
}
void watcher_Changed(object sender, FileSystemEventArgs e)
{
if (e.ChangeType != WatcherChangeTypes.Created) return;
FileDropped(e.FullPath);
}
public void Start()
{
watcher.EnableRaisingEvents = true;
}
public void Stop()
{
watcher.EnableRaisingEvents = false;
}
}
간단한 형태 :
public partial class Form1 : Form
{
Monitor monitor = new Monitor();
public Form1()
{
InitializeComponent();
FormClosing += new FormClosingEventHandler(Form1_FormClosing);
Load += new EventHandler(Form1_Load);
monitor.FileDropped += new Monitor.FileDroppedEvent(monitor_FileDropped);
monitor.Error += new Monitor.ErrorEvent(monitor_Error);
}
void Form1_Load(object sender, EventArgs e)
{
monitor.Start();
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
monitor.Stop();
}
void monitor_Error(Exception ex)
{
listBox1.Items.Add(ex.Message);
}
void monitor_FileDropped(string FullPath)
{
listBox1.Items.Add(FullPath);
}
}
내가 뭘 잘못하고 있니?
코드가 실행중인 사용자가 네트워크 경로에 액세스 할 수 있습니까? – adrianbanks
예. 실행 중이며 액세스 권한이 있습니다 – DontFretBrett
다음 스레드가 유용 할 수 있습니다. http://stackoverflow.com/questions/11219373/filesystemwatcher-to-watch-unc-path – Edin