我的asp net mvc应用程序中有FileSystemWatcher。它会在更改后检测.txt文件。我应该在视图页面上显示这些更改。但我无法理解它是如何做到的。因为我如何从FileSystemWatcher调用页面刷新?或者我必须在客户端编写setTimeOut js函数?这是代码:
public class FileWatcher
{
//private int readLinesCount = 0;
private string watchedPath;
public FileWatcher(string watchedPath)
{
this.watchedPath = watchedPath;
InitFileSystemWatcher();
}
private void InitFileSystemWatcher()
{
FileSystemWatcher oFileWatcher = new FileSystemWatcher();
//Set the path that you want to monitor.
oFileWatcher.Path = watchedPath;
//Set the Filter Expression.
oFileWatcher.Filter = "*.txt";
oFileWatcher.Changed += new FileSystemEventHandler(FileSystemWatcherUpdated);
oFileWatcher.Error += Watcher_Error;
oFileWatcher.EnableRaisingEvents = true;
}
private void Watcher_Error(object sender, ErrorEventArgs e)
{
// Watcher crashed. Re-init.
InitFileSystemWatcher();
}
void FileSystemWatcherUpdated(object sender, FileSystemEventArgs e)
{
//A file has been changed in the monitor directory.
try
{
RecordEntry(e.Name);
}
catch (Exception)
{
InitFileSystemWatcher();
}
}
private void RecordEntry(string fileName)
{
BLogic bll = new BLogic();
bll.AddParamsByFileWatcher(fileName);
}
}
这是一种BLogic的方法:
public void AddParamsByFileWatcher(string fileName)
{
if (fileName != null)
{
HomeController controller = new HomeController();
controller.Server.TransferRequest("/Home/Monitoring", false);
}
}