filesystemwacher

 public void Run()
        {
            var watch = new DelayedFileSystemWatcher(@"d:\test\sz")
            {
                EnableRaisingEvents = true,
                IncludeSubdirectories = true,
                NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite | NotifyFilters.CreationTime
                | NotifyFilters.DirectoryName | NotifyFilters.LastAccess,
                Filter = "*.*"
            };
            watch.Created += new FileSystemEventHandler(CreateWatch);
            //watch.Changed += new FileSystemEventHandler(CreateWatch);
            //try
            //{
            //    var watch = new FileSystemWatcher(@"d:\test\sz")
            //    {
            //        EnableRaisingEvents = true,
            //        IncludeSubdirectories = false,
            //        NotifyFilter = NotifyFilters.DirectoryName,
            //    };
            //    watch.Created += new FileSystemEventHandler(fn1);
            //    watch.Renamed += new RenamedEventHandler(fn2);
            //}
            //catch(Exception ex)
            //{
            //    Console.WriteLine(ex);
            //}
        }

        DateTime dt = DateTime.MinValue;
        Dictionary<string, DateTime> d = new Dictionary<string, DateTime>();
        private void CreateWatch(object sender, FileSystemEventArgs e)
        {
            string s = DateTime.Now.Date.ToString("yyyyMMdd");
            if (Directory.Exists(e.FullPath))
                return;
            string filename = e.Name.ToLower().Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[1];
            string foldername = e.Name.ToLower().Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[0];
            if (foldername != s)
                return;
            if (!filename.EndsWith("326") && !filename.EndsWith("327"))
                return;
            if (!filename.StartsWith("abc"))
                return;
            DateTime lastRead = File.GetLastWriteTime(e.FullPath);
            if (lastRead.Date.ToString("yyyyMMdd") != DateTime.Now.Date.ToString("yyyyMMdd"))
                return;
            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                if (dt.Date.ToString("yyyyMMdd") == DateTime.Now.Date.ToString("yyyyMMdd"))
                    return;
                dt = lastRead;
                //fn(filename);
                Console.WriteLine("creat " + lastRead);
                //Console.WriteLine(e.FullPath);
                //Console.WriteLine(e.Name);
            }
            //if (e.ChangeType == WatcherChangeTypes.Changed)
            //{
            //    if (dt != lastRead)
            //    {
            //        if (d[filename] != lastRead)
            //        {
            //            dt = lastRead;
            //            Console.WriteLine("change" + lastRead);
            //        }
            //    }
            //}
        }

        private void fn(string name)
        {
            DirectoryInfo TheFolder = new DirectoryInfo(@"d:\test\20130326");
            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                if (d.ContainsKey(name))
                {
                    d[name] = NextFile.LastWriteTime;
                }
                else
                {
                    d.Add(name, NextFile.LastWriteTime);
                }
            }
        }

private void CreateWatch1(object sender, FileSystemEventArgs e)
        {
            string s = DateTime.Now.Date.ToString("yyyyMMdd");
            if (Directory.Exists(e.FullPath))
                return;
            string filename = e.Name.ToLower().Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[1];
            string foldername = e.Name.ToLower().Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[0];
            if (foldername != s)
                return;
            if (!filename.EndsWith("326"))//dbf
                return;
            if (!d.ContainsKey(filename))
                return;
            DateTime lastRead = File.GetLastWriteTime(e.FullPath);
            if (lastRead.Date.ToString("yyyyMMdd") != DateTime.Now.Date.ToString("yyyyMMdd"))
                return;
            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                if (d[filename].Date.ToString("yyyyMMdd") == DateTime.Now.Date.ToString("yyyyMMdd"))
                    return;
                d[filename] = lastRead;
                int count = d.Values.Where(p => p.Date.ToString("yyyyMMdd") == DateTime.Now.Date.ToString("yyyyMMdd")).ToList().Count;
                //fn(filename);
                if (count == 3)
                {
                    Console.WriteLine("creat " + lastRead);
                }
                //Console.WriteLine(e.FullPath);
                //Console.WriteLine(e.Name);
            }
        }

 

posted on 2013-03-27 22:00  gjcn  阅读(257)  评论(0)    收藏  举报

导航