我已经尝试过使用MS和SO作为参考的大约100种代码变体,但无法触发文件。实际上,这并不完全准确。我是第一次,但是只有第一次。
这是一个简单的FileSystemWatcher,我正在寻找要创建的文件(可以直接通过另一个进程创建,也可以是被复制的文件。
这是我的代码,我想念什么?
public Form_Status() {
InitializeComponent();
Show();
this.lblStatusText.Text = "Creating Jobs";
Watch(@"\\erpsql\p21shares\PurchasingSave");
}
private static void Watch(string path) {
// Create a new FileSystemWatcher and set its properties.
using (FileSystemWatcher watcher = new FileSystemWatcher()) {
watcher.Path = path;
// LastWrite Triggers
watcher.NotifyFilter = NotifyFilters.LastWrite;
// Only watch text files.
watcher.Filter = "*.*";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
}
private static void OnChanged(object source, FileSystemEventArgs e) {
// Do Something
MessageBox.Show("Created Job: {0}", e.Name);
Console.WriteLine($"File: {e.FullPath} {e.ChangeType}");
}