用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

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

2016-08-14 作者: 小章举报

[c#]代码库

using System;
using System.Collections;
using System.IO;
using System.Threading;
using System.Windows.Forms;
 
namespace MonitorDirectory
{
    class Program
    {
        private static FileSystemWatcher FileSystemWatcher1 = null;
        private static Hashtable fileTable = new Hashtable();
 
        [STAThread]
        public static void Main(string[] args)
        {
            FolderBrowserDialog dilog = new FolderBrowserDialog();
            dilog.Description = "请选择要监视的文件夹";
            if(dilog.ShowDialog() == DialogResult.OK || dilog.ShowDialog() == DialogResult.Yes)
            {
                string path=dilog.SelectedPath;
                FileSystemWatcher1 = new FileSystemWatcher();
                FileSystemWatcher1.Path = path;
                FileSystemWatcher1.Filter = "*.tmp";
                FileSystemWatcher1.IncludeSubdirectories = true;
                FileSystemWatcher1.Created += new FileSystemEventHandler(FileSystemWatcher1_Created);
                FileSystemWatcher1.Changed += new FileSystemEventHandler(FileSystemWatcher1_Changed);
                FileSystemWatcher1.Renamed += new RenamedEventHandler(FileSystemWatcher1_Renamed);
                FileSystemWatcher1.EnableRaisingEvents=true;
                //加入任务
                foreach(string file in Directory.GetFiles(path,"*.txt"))
                {
                    Tasks task = new Tasks();
                    task.filepathname=file;
                    task.Push();
                }
                Tasks t = new Tasks();
                Thread th = new Thread(new ThreadStart(t.ThreadWork));
                th.Start();  
                Console.Read();
                th.Abort();
                FileSystemWatcher1.EnableRaisingEvents = false;
                FileSystemWatcher1.Dispose();
                FileSystemWatcher1 = null;
                return;
            }
        }
 
        private static void FileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
        {
            Monitor.Enter( fileTable.SyncRoot );
            try
            {
                fileTable.Add(e.FullPath,false);
                Console.WriteLine("文件"+e.FullPath+"被创建");
            }
            finally
            {
                Monitor.Exit( fileTable.SyncRoot );
            }
        }
 
        private static void FileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
        {
            if(fileTable.ContainsKey(e.FullPath) && !(bool)fileTable[e.FullPath])
            {
                Monitor.Enter( fileTable.SyncRoot );
                try
                {
                    fileTable[e.FullPath]=true;
                    Console.WriteLine("文件"+e.FullPath+"有数据");
                }
                finally
                {
                    Monitor.Exit( fileTable.SyncRoot );
                }
            }
        }
 
        private static void FileSystemWatcher1_Renamed(object sender, RenamedEventArgs e)
        {
            if(fileTable.ContainsKey(e.OldFullPath) && (bool)fileTable[e.OldFullPath])
            {
                Monitor.Enter( fileTable.SyncRoot );
                try
                {
                    fileTable.Remove(e.OldFullPath);
                    Console.WriteLine("文件"+e.FullPath+"被处理");
                    Tasks task = new Tasks();
                    task.filepathname=e.FullPath;
                    task.Push();
                }
                finally
                {
                    Monitor.Exit( fileTable.SyncRoot );
                }
            }
        }
    }
}

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...