推荐文章

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符
C#监控程序启动和关闭

C#监控程序启动和关闭

C#监控程序启动和关闭using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;using System.Threading;namespace ProcessListener{ class Program { static void Main(stri
C#操作USB口的摄像头

C#操作USB口的摄像头

private const uint BM_CLICK = 0xF5; 鼠标点击的消息,对于各种消息的数值,查API手册,也可用VS2010自带的SPY++ [DllImpt("user32.dll", EntryPoint = "SendMessage", SetLastErr = true, Set = Set.Auto)]private static extern int SendMe
C# FTP操作

C# FTP操作

C# FTP操作
C#文件监控对象FileSystemWatcher

C#文件监控对象FileSystemWatcher

使用C#文件监控对象FileSystemWatcher对文件夹下的删除、修改、新增

C#文件监控对象FileSystemWatcher

日期:2018-07-05 点击:2127 来源:百度

public static FileSystemWatcher watcher = new FileSystemWatcher();
watcher 主要事件:
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);


using System;
using System.IO;
 
namespace test
{
    class Program
    {
        static void Main(string[] args)
        { 
            WatcherStrat(@"C:\test", "*.txt");
            //由于是控制台程序,加个输入避免主线程执行完毕,看不到监控效果
            Console.ReadKey(); 
        }
 
       
 
        private static void WatcherStrat(string path, string filter)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = path; 
            watcher.Filter = filter; 
            watcher.Changed += new FileSystemEventHandler(OnProcess);
            watcher.Created += new FileSystemEventHandler(OnProcess);
            watcher.Deleted += new FileSystemEventHandler(OnProcess);
            watcher.Renamed += new RenamedEventHandler(OnRenamed); 
            watcher.EnableRaisingEvents = true;
        } 
        private static void OnProcess(object source, FileSystemEventArgs e)
        { 
            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                OnCreated(source, e); 
            }
            else if (e.ChangeType == WatcherChangeTypes.Changed)
            {
                OnChanged(source, e);
             }
            else if (e.ChangeType == WatcherChangeTypes.Deleted)
            {
                OnDeleted(source, e); 
            }
        }
 
        private static void OnCreated(object source, FileSystemEventArgs e)
        { 
            Console.WriteLine("文件新建事件处理逻辑");             
        }
 
        private static void OnChanged(object source, FileSystemEventArgs e)
        { 
            Console.WriteLine("文件改变事件处理逻辑");
        }
 
        private static void OnDeleted(object source, FileSystemEventArgs e)
        { 
            Console.WriteLine("文件删除事件处理逻辑");
        }
 
        private static void OnRenamed(object source, RenamedEventArgs e)
        { 
            Console.WriteLine("文件重命名事件处理逻辑");
        } 
    }
}


这篇文档对您是否有帮助?

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符
C#监控程序启动和关闭

C#监控程序启动和关闭

C#监控程序启动和关闭using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;using System.Threading;namespace ProcessListener{ class Program { static void Main(stri
C#操作USB口的摄像头

C#操作USB口的摄像头

private const uint BM_CLICK = 0xF5; 鼠标点击的消息,对于各种消息的数值,查API手册,也可用VS2010自带的SPY++ [DllImpt("user32.dll", EntryPoint = "SendMessage", SetLastErr = true, Set = Set.Auto)]private static extern int SendMe
C# FTP操作

C# FTP操作

C# FTP操作
C#文件监控对象FileSystemWatcher

C#文件监控对象FileSystemWatcher

使用C#文件监控对象FileSystemWatcher对文件夹下的删除、修改、新增