0

当 watcher.created 检测到有新的 dll 时,我想显示新的文件路径

我可以在初始加载时设置 watcher_change() 的值,但我不确定如何从那一点更改列出的值。当我使用由 watcher_change(object sender, FileSystemEventArgs e) 创建时,我可以看到 filePaths 具有我需要的值,我只是不确定如何让它们显示在屏幕上。

public partial class Page : UserControl
{
  private FileWatch f = new FileWatch();

  public Page()
  {     
    ListBox.DataContext = f.watcher_change();
  }
}


public class FileWatch
{
  public FileWatch()
  {
    var watcher = 
          new FileSystemWatcher {Path = @"C:\", EnableRaisingEvents = true};
    watcher.Created += (o, args) => watcher_change(o, args);              
  }

  public string[] watcher_change(object sender, FileSystemEventArgs e)
  {
    string[] filePaths = Directory.GetFiles(@"C:\", "*.dll");
    return filePaths;
  }


  public string[] watcher_change()
  {
    string[] filePaths = Directory.GetFiles(@"C:\", "*.dll");
    return filePaths;
  }
}
4

2 回答 2

0

您需要在您的FileWatch类中定义一个事件,您可以从您的watcher_change方法中触发该事件。然后,您Page必须订阅此活动。

于 2013-11-05T10:19:40.553 回答
0

要建立在 CodeCaster 所说的基础上,您可以包装观察者事件并将其伪装成您自己的:

 public class FileWatch
 {
    public FileWatch()
    {
       var watcher = new FileSystemWatcher {Path = @"C:\", EnableRaisingEvents = true};
       watcher.Created += (o, args) => watcher_change(o, args);              
    }

    public string[] watcher_change(object sender, FileSystemEventArgs e)
    {
       string[] filePaths = Directory.GetFiles(@"C:\", "*.dll");
       return filePaths;
    }

    public event EventHandler<object,FileSystemEventArgs>  YourEvent
    {
        add
        {
            watcher.Created += value;
        }
        remove
        {
            watcher.Created -= value;
        }
    } 
}
于 2013-11-05T10:28:23.110 回答