Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改FileWatch上其他类的值_C#_Wpf - Fatal编程技术网

C# 更改FileWatch上其他类的值

C# 更改FileWatch上其他类的值,c#,wpf,C#,Wpf,我想在watcher.created检测到有新的dll时显示新的文件路径 我可以在初始加载时设置watcher_change()的值,但不确定如何更改 从那一点开始,我们就列出了答案。当我使用由watcher\u创建的更改(对象发送者、文件系统管理员) 我可以看到文件路径有我需要的值,我只是不知道如何让它们显示在屏幕上 public partial class Page : UserControl { private FileWatch f = new FileWatch(); pub

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

我可以在初始加载时设置watcher_change()的值,但不确定如何更改 从那一点开始,我们就列出了答案。当我使用由watcher\u创建的更改(对象发送者、文件系统管理员) 我可以看到文件路径有我需要的值,我只是不知道如何让它们显示在屏幕上

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;
  }
}

您需要在
FileWatch
类中定义一个事件,该事件由
watcher\u change
方法触发。然后,您的
页面必须订阅此事件。

若要按照CodeCaster的说法构建apon,您可以包装观察者事件并将其伪装为您自己的事件:

 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;
        }
    } 
}
公共类文件监视
{
公共文件监视()
{
var-watcher=newfilesystemwatcher{Path=@“C:\”,EnableRaisingEvents=true};
watcher.Created+=(o,args)=>watcher\u change(o,args);
}
公共字符串[]观察者\u更改(对象发送者、文件系统目标)
{
字符串[]filepath=Directory.GetFiles(@“C:\”,“*.dll”);
返回文件路径;
}
公共事件事件处理程序YourEvent
{
添加
{
watcher.Created+=值;
}
去除
{
watcher.Created-=值;
}
} 
}

非常感谢您的帮助,我使用了一些示例,并成功地将其付诸实施。感谢您的示例,它帮助了我