C# FileSystemWatch使用
来源:互联网 发布:c语言printf输出汉字 编辑:程序博客网 时间:2024/05/19 17:09
MSDN上一段关于使用FileSystemWatch类的例子,今天写的一个功能使用到监视文件系统的应用,看到这段代码,很有启发,留作纪念。代码如下:
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;
watcher.IncludeSubdirectories = True; // 这个属性用于指定是否监视指定目录中的子目录,很有用
// Only watch text files.
watcher.Filter = "*.*";
// 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);
}
}
转自:http://blog.csdn.net/guoxubin2005/article/details/4154095
- C# FileSystemWatch使用
- FileSystemWatch使用示例
- C# 使用
- C# Builder 使用心得
- 开始使用C#
- 使用c#之感想
- C#中使用网上邻居
- C#中使用Excel
- C#中使用SendMessage
- 再次使用c#
- C#中使用网上邻居
- C#中使用委托
- c#中使用多线程
- c#使用有感
- c#中使用多线程
- c#中使用多线程
- 使用C#画党徽
- C#字符串的使用
- 链表的常见用法
- 项目小总结
- [Rx86OS-III] 由实模式切换到保护模式
- struts2 能进入action 不执行execute的问题
- JQUERY MOBILE 控件的一些refresh
- C# FileSystemWatch使用
- 有些代码不应该被忘记,也没有源代码不应该被记住。
- installshield2009打包工具使用方法
- Vijava 学习笔记之 VirtualMachine(状态(通电状态))
- 简单巴什博弈
- 使用命令的脚本来停止或启动IIS中的某个站点
- How to share on Facebook obout iOS?
- ThreadLocal
- 20:Valid Parentheses【栈】【字符串】