有关FileAndFolderWatcher的问题 [英] A question about FileAndFolderWatcher

查看:63
本文介绍了有关FileAndFolderWatcher的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我发现当我调用DirectoryInfo.GetFileSystemInfos方法时,这可能导致FileAndFolderWatcher引发更改事件.我在这里有一些测试代码

I found that it may cause FileAndFolderWatcher to rasing a change event when I call DirectoryInfo.GetFileSystemInfos method. I have some tesing codes here

string path = "D:\\Test\\";
private void button1_Click(object sender, EventArgs e)
{
	FileSystemWatcher watcher = new FileSystemWatcher();
	watcher.Path = path;
	watcher.InternalBufferSize = 40960;
	watcher.IncludeSubdirectories = true;
	//watcher.Created += new FileSystemEventHandler(watcher_Created);
	watcher.Changed += new FileSystemEventHandler(watcher_Changed);
	//watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
	//watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
	//watcher.Error += new ErrorEventHandler(watcher_Error);
	watcher.EnableRaisingEvents = true;
}
private void watcher_Changed(object sender, FileSystemEventArgs e)
{
	Console.WriteLine("Change: " + e.FullPath);
}
private void button2_Click(object sender, EventArgs e)
{
	DirectoryInfo di = new DirectoryInfo(path);
	GetSubDirectories(di);
}
private void GetSubDirectories(DirectoryInfo di)
{
	FileSystemInfo[] fsiChildren = di.GetFileSystemInfos();
	foreach (FileSystemInfo fsi in fsiChildren)
	{
		if (fsi is DirectoryInfo)
		{
			GetSubDirectories((DirectoryInfo)fsi);
		}
		else if (fsi is FileInfo)
		{
		}
	}
}

启动程序后,将包含一些子文件夹和文件的文件夹复制到D:\ Test,然后单击button1启动FileAndFolderWatcher.然后,我单击button2,我可以看到显示的一些记录.为什么?除了我的GetSubDirectories方法外,它什么都不做

After I start the program, I copy a folder with some sub folders and files into D:\Test, then I click button1 to start FileAndFolderWatcher. Then I click button2 and I can see some records shown. Why? It does nothing in my GetSubDirectories method except calling DirectoryInfo.GetFileSystemInfos.

推荐答案

将搜索参数放入GetFileSystemInfos

put search parameter to GetFileSystemInfos

di.GetFileSystemInfos("*",SearchOption.AllDirectories)

如果不通过,默认情况下将仅从顶层目录搜索文件.

If you do not pass, default will search files from top directory only.

希望这会有所帮助.


这篇关于有关FileAndFolderWatcher的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆