FileSystemWatcher Onchange return 值

FileSystemWatcher Onchage return value

我有一个 FileSystemEventHandler onchange 从我的文件中读取数据,现在我需要 return 这个数据,因为我正在使用处理程序。现在我的代码可以工作,但没有 return 任何东西,所以我的前端没有更新的数据。 这是我的问题:我如何 return data?

谢谢

public static string data = null;
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static string 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());
    }
    return data;

}

private static void OnChanged(object source, FileSystemEventArgs e)
{
    data = FileManager.Read();
}

FileSystemWatcher是一种事件驱动机制。您不需要 return 您的 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
}