C# FileSystemWatcher 如何监控Changed事件到底是文件大小改变还是文件属性变化还是访问时间变化呢?
解决方案 »
- 怎么做带链接的DropDownList下拉框
- 初学C# ASP与C#区别是什么 asp中response.write可以输出字符串,C#中???
- 编程爱好者请进,共同探讨一下一个SQL语句的写法!!!
- DataSet 有正确答案就给分
- 请教:用VS2005直接制作的Icon1.ico文件,为何会显示空白,有人可以帮忙吗?----在线等待
- 怎样在右键单击Form标题栏时显示自定义的菜单项?
- 求Lostinet的SqlScope6.0组件一份
- c#求学习资料!动态编译和编译器要详细要全!~
- httpwatch抓取新浪博客登陆脚本
- 我想让我的软件退出的时候提示用户要不要关机,如果是就关闭计算机。
- XPath查询不同属性
- C# Winform中的textbox控件回车事件疑问。
NotifyFilter属性为可组合 NotifyFilters 枚举的成员,以同时监视多种类型的更改。例如,可监视文件大小方面的更改和 LastWrite 时间方面的更改。只要文件或文件夹大小有变化或者如果文件或文件夹的 LastWrite 时间有变化,就会引发事件。
public class Watcher
{ public static void Main()
{
Run(); } [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs(); // If a directory is not specified, exit program.
if(args.Length != 2)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: Watcher.exe (directory)");
return;
} // Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt"; // Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed); // Begin watching.
watcher.EnableRaisingEvents = true; // Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
} // Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
} private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
我也知道可以设置多种监控
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
其中:e.ChangeType就是你需要的,ChangeType 地取值为WatcherChangeTypes 值之一,表示在文件系统中检测到的更改种类。
参考代码:
if(e.ChangeType == WatcherChangeTypes.Changed)
{...}
e.ChangeType里只有4种变化 其中一个就是Changed
我是知道发生了Changed事件
可是由什么引起的呢? 改文件大小了? 访问时间修改了?
还是文件属性修改了?
FileSystemWatcher watcherSize = new FileSystemWatcher();
watcherSize.Changed += new FileSystemEventHandler(watcherSize_Changed);
watcherSize.NotifyFilter = NotifyFilters.Size;
FileSystemWatcher watcherLastAccess = new FileSystemWatcher();
watcherLastAccess.NotifyFilter = NotifyFilters.LastAccess;
watcherLastAccess.Changed += new FileSystemEventHandler(watcherLastAccess_Changed);
该事件响应函数的参数:e.ChangeType就是对应的变化了
if(e.ChangeType == WatcherChangeTypes.Changed)
{...}