C# 更改FileWatch上其他类的值
我想在watcher.created检测到有新的dll时显示新的文件路径 我可以在初始加载时设置watcher_change()的值,但不确定如何更改 从那一点开始,我们就列出了答案。当我使用由watcher\u创建的更改(对象发送者、文件系统管理员) 我可以看到文件路径有我需要的值,我只是不知道如何让它们显示在屏幕上C# 更改FileWatch上其他类的值,c#,wpf,C#,Wpf,我想在watcher.created检测到有新的dll时显示新的文件路径 我可以在初始加载时设置watcher_change()的值,但不确定如何更改 从那一点开始,我们就列出了答案。当我使用由watcher\u创建的更改(对象发送者、文件系统管理员) 我可以看到文件路径有我需要的值,我只是不知道如何让它们显示在屏幕上 public partial class Page : UserControl { private FileWatch f = new FileWatch(); pub
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-=值;
}
}
}
非常感谢您的帮助,我使用了一些示例,并成功地将其付诸实施。感谢您的示例,它帮助了我