FileSystemWatcher Onchage返回值

时间:2016-11-01 15:39:55

标签: c# file filesystemwatcher

我有一个unlist FileSystemEventHandler从我的文件中读取数据,现在我需要在处理程序时返回这些数据。现在我的代码可以工作,但不会返回任何内容,所以我的前端没有更新的数据。 这是我的问题:如何退回onchange

由于

data

1 个答案:

答案 0 :(得分:0)

FileSystemWatcher是一种事件驱动机制。您无需从Run()方法返回任何内容 - 由于OnChanged()事件处理程序的更改,您需要执行任何操作。试着看看API for FileSystemEventArgs

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
    try
    {
        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        //watcher.Path = System.IO.Directory.GetCurrentDirectory();
        watcher.Path = Path.Combine(HttpRuntime.AppDomainAppPath, "view");
        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        watcher.Filter = "info.txt";

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);

        // Begin watching.
        watcher.EnableRaisingEvents = true;
    }
    catch (Exception ex)
    {
        Console.Write(ex.ToString());
    }
}


private static void OnChanged(object source, FileSystemEventArgs e)
{
    string fileText = File.ReadAllText(e.FullPath);
    // do whatever you want to do with fileText
}