• 多线程监控文件夹,FlieSystemWatcher,并使用共享函数


    发表于: 2011-01-06 09:55:47

     
    C# code
     
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.IO;
    using System.Collections;
     
    namespace Thread_FileSystemWatcher
    {
        class Program
        {
            private static Thread[] threads;
            private static string[] pPath;
             
            static void Main(string[] args)
            {
                threadsPEIZHI();
                while (Console.Read() != 'q') ;
     
            }
     
            static void threadsPEIZHI()
            {
                try
                {
                    pPath = new string[2];
                    pPath[0] = "c:\";
                    pPath[1] = "e:\";
     
                    threads = new Thread[pPath.Length];
                    for (int i = 0; i <= threads.Length-1; i++)
                    {
                        threads[i] = new Thread(Run);
                        threads[i].Name = pPath[i];
                        threads[i].Start();
                        Console.WriteLine(threads[i].Name);
                    }
                }
                catch(Exception Ex)
                {
                    Console.WriteLine(Ex.Message);
                }
            }
     
            static void Run()
            {
                Run(Thread.CurrentThread.Name);
            }
     
            static void Run(string pPath)
            {
                FileSystemWatcher fsw = new FileSystemWatcher(pPath);
                fsw.Filter = "*.*";//监控所有类型,包括子文件夹
                fsw.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.LastAccess | NotifyFilters.LastWrite;
     
                fsw.Changed += new FileSystemEventHandler(OnChanged);
                fsw.Created += new FileSystemEventHandler(OnCreated);
                fsw.Deleted += new FileSystemEventHandler(OnDeleted);
                fsw.Renamed += new RenamedEventHandler(OnRenamed);
     
                fsw.EnableRaisingEvents = true;//开启监控
     
            }
     
            static void OnChanged(object source,FileSystemEventArgs e)
            {
                Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
            }
     
            static void OnCreated(object source,FileSystemEventArgs e)
            {
                Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
            }
     
            static void OnDeleted(object source,FileSystemEventArgs e)
            {
                Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
            }
     
            static void OnRenamed(object source, RenamedEventArgs e)
            {
                Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
            }
     
        }
    }

    请指教!
  • 相关阅读:
    svn更新出错,Previous operation has not finished; run 'cleanup' if it was interrupted
    angular转换日期格式问题
    eclipse自动补全和取消空格自动补全功能
    对枚举和静态常量的理解
    对Spring的IOC、DI、AOP的理解
    maven库因为网络原因jar下载失败
    java new一个对象的过程
    shelve模块读写文件报错
    $http.get(...).then(...).error is not a function
    jQuery动画的先后执行
  • 原文地址:https://www.cnblogs.com/skyay/p/3875188.html
Copyright © 2020-2023  润新知