0

I would like to show the the new filePaths when watcher.created detects there is a new dll

I am able to set the values from watcher_change() on initial load but I am not sure how to change the listed from that point. When I use created by watcher_change(object sender, FileSystemEventArgs e) I can see that filePaths has the values I need, I am just not sure how to get them to display on screen.

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;
  }
}
JERRY-the chuha
  • 592
  • 1
  • 4
  • 16
NooBalus
  • 5
  • 2

2 Answers2

0

You need to define an event in your FileWatch class, which you fire from your watcher_change method. Your Page must then subscribe to this event.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    Thank you very much for your help I used http://stackoverflow.com/questions/803242/understanding-events-and-event-handlers-in-c-sharp for examples and I manage to get it to work – NooBalus Nov 05 '13 at 12:39
0

To build apon what CodeCaster said you can wrap the watcher event and disguise it as your own:

 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;
        }
    } 
}
eran otzap
  • 12,293
  • 20
  • 84
  • 139