【问题标题】:FileSystemWatcher Onchage return valueFileSystemWatcher Onchange 返回值
【发布时间】:2016-11-01 15:39:55
【问题描述】:

我有一个FileSystemEventHandler onchange 从我的文件中读取数据,现在我需要在使用处理程序时返回这些数据。现在我的代码可以工作但没有返回任何东西,所以我的前端没有更新的数据。 这是我的问题:我怎样才能返回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();
}

【问题讨论】:

  • 您的意思是要阻止Run() 直到OnChanged() 被提出?
  • OnChanged 事件触发设置data 之前很久,您的运行方法将返回 data == null。
  • 如果您不需要阻止Run(),那么您不应该期望它返回数据来更新用户界面。您应该直接在OnChanged() 中更新您的用户界面,或者在必须在 OnChanged() 中调用的另一种方法中更新您的用户界面,该方法将为您读取数据。
  • 你有什么样的前端?,你一定有an Observer, Pub/Sub, and Data Binding
  • @Byron 我正在使用 angularJS 一个函数来调用使用处理程序的方法并返回 daa

标签: c# file filesystemwatcher


【解决方案1】:

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
}

【讨论】:

  • 那么我怎样才能返回我的数据,在这种情况下不会调用处理程序
  • FileSystemWatcher 将在更改发生时为您调用OnChanged()。然后,由于更改,您可以在处理程序中执行您想做的任何事情。例如,您可以直接从处理程序更新您的 UI。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多