日期:2014-05-17  浏览次数:20877 次

FileSystemWatcher怎么取得监控目录名
本帖最后由 mpy2003 于 2013-11-11 13:05:17 编辑
这个控件使用的时候会给出监控目录fileSystemWatcher.Path
如果是固定的一个目录比较好知道,现在我是动态加载的一系列fileSystemWatcher.Path
见我另外一个贴子http://bbs.csdn.net/topics/390637633

foreach (ListViewItem i in this.listView.Items)
{
      this.fileSystemWatcher = new FileSystemWatcher();
      this.fileSystemWatcher.Path = i.SubItems[2].Text;//取ListView中的值为控件文件夹
   this.fileSystemWatcher.Created += new FileSystemEventHandler(watcher_Created);
   this.fileSystemWatcher.EnableRaisingEvents = true;
}


private void watcher_Created(object sender, FileSystemEventArgs e)
{
   在这里我想知道是哪个监控目录发生,想取到那个Path怎么写?
}
fileSystemWatcher

------解决方案--------------------
private void watcher_Created(object sender, FileSystemEventArgs e)
{
      var path = (sender as FileSystemWatcher).Path;
}
------解决方案--------------------
你本身默认的路径是this.fileSystemWatcher1.Path =@“C:\a\b”,下面还有子目录,要取主目录,用this.fileSystemWatcher1.Path就可阿,
------解决方案--------------------
引用:
private void watcher_Created(object sender, FileSystemEventArgs e)
{
      var path = (sender as FileSystemWatcher).Path;
}

这是应该是可以的
只要你得到的对应的FileSystemWatcher,并结合FileSystemEventArgs,可以拿到你想要的任何数据,如果不行,你甚至可以写个新类,继承FileSystemWatcher,并添加一些属性,来记录你想要的数据

这么多楼了,楼主整理下,这个应该是可以解决的