Systemfilewatcher未正确触发已删除的事件

时间:2015-12-09 15:21:06

标签: c# wpf

我有一个filewatcher:

private void initFileWatcherActive()
    {
        try
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = Global.Directory + Global.Machine + "\\" + Machines.Directories.Aktiv;

            watcher.NotifyFilter = NotifyFilters.Attributes |
            NotifyFilters.CreationTime |
            NotifyFilters.DirectoryName |
            NotifyFilters.FileName |
            NotifyFilters.LastAccess |
            NotifyFilters.LastWrite;

            watcher.IncludeSubdirectories = false;
            watcher.Filter = "*.*";
            watcher.InternalBufferSize = 24000;


            watcher.Created -= new FileSystemEventHandler(OnChangedActive);
            watcher.Deleted -= new FileSystemEventHandler(OnChangedActive);

            watcher.Created += new FileSystemEventHandler(OnChangedActive);
            watcher.Deleted += new FileSystemEventHandler(OnChangedActive);
            watcher.EnableRaisingEvents = true;
        }
        catch (Exception ex)
        {
            setStatus("Filewatcher konnte nicht initialisiert werden: " + ex.Message, Level.ERROR);
        }
    }

通过此电话:

   private void OnChangedActive(object source, FileSystemEventArgs e)
    {
        try
        {
            switch (e.ChangeType)
            {
                case WatcherChangeTypes.Created: break;

                case WatcherChangeTypes.Deleted:



                    break;
                default:
                    break;
            }   
        }
        catch (Exception ex)
        {
            this.Dispatcher.BeginInvoke(new Action(() => setStatus(ex.Message, Level.ERROR)));
        }

    }

如果我在Windows资源管理器中添加或删除文件,一切正常。 但在我的场景中,另一个.net程序使用以下代码添加文件:

public static void add(String path, MachineOrder machineOrder, String fileExtention)
    {

        String filePath = path;
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.AppendLine("Auftragsnummer;" + machineOrder.Ordernumber);
        stringBuilder.AppendLine("Positionsnummer;" + machineOrder.Positionnumber);
        stringBuilder.AppendLine("Aenderungsdatum;" + machineOrder.Editdate.ToString("yyyy-MM-dd hh:mm:ss"));
        stringBuilder.AppendLine("Menge;" + machineOrder.Amount.ToString());
        stringBuilder.AppendLine("Menge fertig;" + machineOrder.AmountReady.ToString());
        stringBuilder.AppendLine("Termin;" + machineOrder.Deliverydate.ToString("yyyy-MM-dd"));
        stringBuilder.AppendLine("Kundennummer;" + machineOrder.Customernumber);
        stringBuilder.AppendLine("Kundenname;" + machineOrder.Customername);
        stringBuilder.AppendLine("Kdauf.-nummer;" + machineOrder.CustomerOrdernumber);
        stringBuilder.AppendLine("Kdauf.-positionsnummer;" + machineOrder.CustomerOrdernumber);
        stringBuilder.AppendLine("Modell;" + machineOrder.Modell);


        foreach (KeyValuePair<String, string> characteristic in machineOrder.Charakteristics)
        {
            stringBuilder.AppendLine(characteristic.Key + ";" + characteristic.Value);
        }


        String filename = FilenameBuilder.buildFilename(machineOrder, fileExtention);

        filePath = filePath + "\\" + filename;
        System.IO.File.WriteAllText(filePath, stringBuilder.ToString());

    }

现在filewatcher跳转到创建,删除,创建,删除等等。我不知道为什么

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可能无意中实例化了多个FileSystemWatcher个实例。

initFileWatcherActive创建一个新实例,删除事件处理程序,并添加新的处理程序。您尝试从全新实例中删除处理程序的事实使我认为您多次调用此函数,可能打算从现有实例中删除处理程序。这将导致多个观察者观看同一目录,并使事件看起来不止一次。

您可能应该在更高的范围内声明一个观察者,并且只实例化一次。