Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Global.asax文件中的事件侦听器脚本需要不断运行_C#_Asp.net_Filesystemwatcher - Fatal编程技术网

C# Global.asax文件中的事件侦听器脚本需要不断运行

C# Global.asax文件中的事件侦听器脚本需要不断运行,c#,asp.net,filesystemwatcher,C#,Asp.net,Filesystemwatcher,我这样做对吗 问题: 编写一个asp.net脚本,不断检查服务器上的目录是否有任何更改 我提出的解决方案: 编写一个侦听器,检查global.asax文件中目录中是否有任何已更改的文件 FileSystemWatcher watcher; //string directoryPath = ""; protected void Application_Start(Object sender, EventArgs e) { string directoryPath = HttpContex

我这样做对吗

问题:
编写一个asp.net脚本,不断检查服务器上的目录是否有任何更改

我提出的解决方案:
编写一个侦听器,检查global.asax文件中目录中是否有任何已更改的文件

FileSystemWatcher watcher;
//string directoryPath = "";

protected void Application_Start(Object sender, EventArgs e)
{
    string directoryPath = HttpContext.Current.Server.MapPath("/xmlFeed/");
    watcher = new FileSystemWatcher();
    watcher.Path = directoryPath;
    watcher.Changed += somethingChanged;

    watcher.EnableRaisingEvents = true;
}
void somethingChanged(object sender, FileSystemEventArgs e)
{
    DateTime now = DateTime.Now;
    System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath("/debug.txt"), "(" + "something is working" + ")  " + now.ToLongTimeString() + "\n");//nothing is getting written to my file 
}
我遇到的问题:

  • 目录发生更改时,事件处理程序未启动
  • 确保脚本始终在服务器上运行
我对这个问题采取了正确的方法吗

这是我在global.asax文件中的代码

FileSystemWatcher watcher;
//string directoryPath = "";

protected void Application_Start(Object sender, EventArgs e)
{
    string directoryPath = HttpContext.Current.Server.MapPath("/xmlFeed/");
    watcher = new FileSystemWatcher();
    watcher.Path = directoryPath;
    watcher.Changed += somethingChanged;

    watcher.EnableRaisingEvents = true;
}
void somethingChanged(object sender, FileSystemEventArgs e)
{
    DateTime now = DateTime.Now;
    System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath("/debug.txt"), "(" + "something is working" + ")  " + now.ToLongTimeString() + "\n");//nothing is getting written to my file 
}

在网站上这样做并不是文件观察者的理想场所。 但是,您的错误是因为HttpContext.Current在事件处理程序中为null,因为该事件不在asp.net请求管道中

如果您坚持这样做,请按如下方式更改代码:

private FileSystemWatcher watcher;
private string debugPath;
void Application_Start(object sender, EventArgs e)
{
    string directoryPath = HttpContext.Current.Server.MapPath("/xmlFeed/");
    debugPath = HttpContext.Current.Server.MapPath("/debug.txt");
    watcher = new FileSystemWatcher();
    watcher.Path = directoryPath;
    watcher.Changed += somethingChanged;

    watcher.EnableRaisingEvents = true;
}
void somethingChanged(object sender, FileSystemEventArgs e)
{
    DateTime now = DateTime.Now;
    System.IO.File.AppendAllText(debugPath, "(something is working)" + now.ToLongTimeString() + "\n");
}

这可能更适合windows服务。您可以让服务监视文件夹,并在发生更改时向您的站点发出请求。我想我也可以使用windows服务,但客户端希望此服务在服务器上运行,或者让asp脚本不断运行,以便他们可以维护或查看页面上的内容。还有别的办法解决这个问题吗。