• [.NET] : 如何监看档案建立完毕


    前言 :

    一般在监控文件夹或是档案异动,大多使用FileSystemWatcher类别
    但FileSystemWatcher在监控档案建立的事件时
    他发出的 Created事件的时间点,是在档案建立开始的当时,而不是档案建立完毕的时间点。
    如果直接使用的话,有时会造成建立档案的线程跟监控档案的线程互相冲突
    造成『由于另一个进程正在使用档案』的例外产生。

    改为本篇文章介绍的使用方法及可避免上述问题

    实作 :

    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using System.IO; 
     
    namespace ConsoleApplication1 
    { 
        public class SampleFolderWatcher 
        { 
            // Properties  
            private string _folderPath = null; 
     
            private FileSystemWatcher _watcher = null; 
     
            private List<string> _creatingFiles = new List<string>(); 
     
     
            // Construction 
            public SampleFolderWatcher(string folderPath) 
            { 
                Require#region Require 
     
                if (folderPath == null) throw new ArgumentNullException(); 
     
                #endregion 
     
                _folderPath = folderPath; 
     
                _watcher = new FileSystemWatcher(); 
                _watcher.Path = _folderPath; 
                _watcher.Created += new FileSystemEventHandler(FileSystemWatcher_Creating); 
                _watcher.Changed += new FileSystemEventHandler(FileSystemWatcher_Changed); 
                _watcher.EnableRaisingEvents = true; 
            } 
     
     
            // FileSystemWatcher Events 
            private void FileSystemWatcher_Creating(object sender, FileSystemEventArgs e) 
            { 
                if (File.Exists(e.FullPath) == true) 
                { 
                    if (_creatingFiles.Contains(e.FullPath) == true) 
                    { 
                        _creatingFiles.Remove(e.FullPath); 
                    } 
                    _creatingFiles.Add(e.FullPath); 
                } 
            } 
     
            private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e) 
            { 
                if (File.Exists(e.FullPath) == true) 
                { 
                    if (_creatingFiles.Contains(e.FullPath) == true) 
                    { 
                        try 
                        { 
                            FileStream stream = File.OpenWrite(e.FullPath); 
                            stream.Close(); 
     
                            _creatingFiles.Remove(e.FullPath); 
     
                            FileSystemWatcher_Created(sender, new FileSystemEventArgs(WatcherChangeTypes.Created, Path.GetDirectoryName(e.FullPath), Path.GetFileName(e.FullPath))); 
                        } 
                        catch 
                        { 
     
                        } 
                    } 
                } 
            } 
     
            private void FileSystemWatcher_Created(object sender, FileSystemEventArgs e) 
            { 
                if (File.Exists(e.FullPath) == true) 
                { 
                    // 档案建立后要做的事 
                } 
            } 
        } 
    }
  • 相关阅读:
    zoj 4120Tokens on the Segments(优先队列+贪心)
    hdu1710 Binary Tree Traversals(二叉树)
    poj3494Largest Submatrix of All 1’s
    poj 2559Largest Rectangle in a Histogram(单调栈简单模板题)
    poj 2492 A Bug's Life(种类并查集)
    差分约束 + spfa + 最长路 [NOI1999] 01串
    Codeforces Round #599 D Yet Another Monster Killing Problem
    CF 1249D1
    [Gym-102346A] 偷偷偷 并查集处理图(坐标)
    [Gym-102346M] 二分答案
  • 原文地址:https://www.cnblogs.com/clark159/p/2205001.html
Copyright © 2020-2023  润新知