I'm trying to monitor a folder in my asp.net mvc5 project. And I thought of putting it in the Startup.cs where I initialize my FileSystemWatcher.
public static void initializeWatcher()
{
string importPath = @"\\server\Exchange\Inbox", importFilterType = "*.xml";
try
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = importPath;
watcher.Filter = ".xml";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
watcher.Created += new FileSystemEventHandler(loadFile);
watcher.Changed += new FileSystemEventHandler(loadFile);
watcher.EnableRaisingEvents = true;
}
catch (Exception e)
{
}
}
private static void loadFile(object source, FileSystemEventArgs e)
{
xmlDocument.LoadXml(e.FullPath);
}
}
Now, the watcher gets created but the 'loadFile' method is not being triggered, no matter what I don in the folder. I followed the example from microsoft: https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx
Let me know what I'm doing wrong.