[C#] 自动监视并处理共享目录中的文本文件并处理 →→→→→进入此内容的聊天室

来自 , 2019-07-06, 写在 C#, 查看 108 次.
URL http://www.code666.cn/view/e727fa59
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6.  
  7. namespace MonitorDirectory
  8. {
  9.     class Program
  10.     {
  11.         private static FileSystemWatcher FileSystemWatcher1 = null;
  12.         private static Hashtable fileTable = new Hashtable();
  13.  
  14.         [STAThread]
  15.         public static void Main(string[] args)
  16.         {
  17.             FolderBrowserDialog dilog = new FolderBrowserDialog();
  18.             dilog.Description = "请选择要监视的文件夹";
  19.             if(dilog.ShowDialog() == DialogResult.OK || dilog.ShowDialog() == DialogResult.Yes)
  20.             {
  21.                 string path=dilog.SelectedPath;
  22.                 FileSystemWatcher1 = new FileSystemWatcher();
  23.                 FileSystemWatcher1.Path = path;
  24.                 FileSystemWatcher1.Filter = "*.tmp";
  25.                 FileSystemWatcher1.IncludeSubdirectories = true;
  26.                 FileSystemWatcher1.Created += new FileSystemEventHandler(FileSystemWatcher1_Created);
  27.                 FileSystemWatcher1.Changed += new FileSystemEventHandler(FileSystemWatcher1_Changed);
  28.                 FileSystemWatcher1.Renamed += new RenamedEventHandler(FileSystemWatcher1_Renamed);
  29.                 FileSystemWatcher1.EnableRaisingEvents=true;
  30.                 //加入任务
  31.                 foreach(string file in Directory.GetFiles(path,"*.txt"))
  32.                 {
  33.                     Tasks task = new Tasks();
  34.                     task.filepathname=file;
  35.                     task.Push();
  36.                 }
  37.                 Tasks t = new Tasks();
  38.                 Thread th = new Thread(new ThreadStart(t.ThreadWork));
  39.                 th.Start();  
  40.                 Console.Read();
  41.                 th.Abort();
  42.                 FileSystemWatcher1.EnableRaisingEvents = false;
  43.                 FileSystemWatcher1.Dispose();
  44.                 FileSystemWatcher1 = null;
  45.                 return;
  46.             }
  47.         }
  48.  
  49.         private static void FileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
  50.         {
  51.             Monitor.Enter( fileTable.SyncRoot );
  52.             try
  53.             {
  54.                 fileTable.Add(e.FullPath,false);
  55.                 Console.WriteLine("文件"+e.FullPath+"被创建");
  56.             }
  57.             finally
  58.             {
  59.                 Monitor.Exit( fileTable.SyncRoot );
  60.             }
  61.         }
  62.  
  63.         private static void FileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
  64.         {
  65.             if(fileTable.ContainsKey(e.FullPath) && !(bool)fileTable[e.FullPath])
  66.             {
  67.                 Monitor.Enter( fileTable.SyncRoot );
  68.                 try
  69.                 {
  70.                     fileTable[e.FullPath]=true;
  71.                     Console.WriteLine("文件"+e.FullPath+"有数据");
  72.                 }
  73.                 finally
  74.                 {
  75.                     Monitor.Exit( fileTable.SyncRoot );
  76.                 }
  77.             }
  78.         }
  79.  
  80.         private static void FileSystemWatcher1_Renamed(object sender, RenamedEventArgs e)
  81.         {
  82.             if(fileTable.ContainsKey(e.OldFullPath) && (bool)fileTable[e.OldFullPath])
  83.             {
  84.                 Monitor.Enter( fileTable.SyncRoot );
  85.                 try
  86.                 {
  87.                     fileTable.Remove(e.OldFullPath);
  88.                     Console.WriteLine("文件"+e.FullPath+"被处理");
  89.                     Tasks task = new Tasks();
  90.                     task.filepathname=e.FullPath;
  91.                     task.Push();
  92.                 }
  93.                 finally
  94.                 {
  95.                     Monitor.Exit( fileTable.SyncRoot );
  96.                 }
  97.             }
  98.         }
  99.     }
  100. }

回复 "自动监视并处理共享目录中的文本文件并处理"

这儿你可以回复上面这条便签

captcha