Filesystem watcher and large files

后端 未结 5 456
感情败类
感情败类 2020-12-02 21:42
var fsw = new FileSystemWatcher(sPath, \"*.PPF\");
fsw.NotifyFilter = NotifyFilters.FileName;
fsw.IncludeSubdirectories = true;
fsw.Created += FswCreated;
fsw.Enable         


        
5条回答
  •  庸人自扰
    2020-12-02 22:06

    Solution found on stackoverflow and modified it a bit.

    static bool IsFileLocked(FileInfo file)
    {
        FileStream stream = null;
    
        try
        {
            stream = file.Open(FileMode.Open, 
                     FileAccess.ReadWrite, FileShare.None);
        }
        catch (IOException)
        {
            //the file is unavailable because it is:
            //still being written to
            //or being processed by another thread
            //or does not exist (has already been processed)
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }
    
        //file is not locked
        return false;
    }
    
    static void FswCreated(object sender, FileSystemEventArgs e)
    {
        string sFile = e.FullPath;
    
        Console.WriteLine("processing file : " + sFile);
    
        // Wait if file is still open
        FileInfo fileInfo = new FileInfo(sFile);
        while(IsFileLocked(fileInfo))
        {
            Thread.Sleep(500);
        }
    
        string[] arrLines = File.ReadAllLines(sFile);
    }
    

提交回复
热议问题