【问题标题】:Filesystem watcher and large files文件系统观察者和大文件
【发布时间】:2010-09-29 13:44:47
【问题描述】:
var fsw = new FileSystemWatcher(sPath, "*.PPF");
fsw.NotifyFilter = NotifyFilters.FileName;
fsw.IncludeSubdirectories = true;
fsw.Created += FswCreated;
fsw.EnableRaisingEvents = true;

static void FswCreated(object sender, FileSystemEventArgs e)
{
  string sFile = e.FullPath;
  string[] arrLines = File.ReadAllLines(sFile);
}

这对于大文件会失败,因为该过程没有完成写入文件。该文件是通过网络复制的,所以我不知道文件的大小。需要什么样的同步才能使其稳健?

【问题讨论】:

    标签: c# io filesystemwatcher


    【解决方案1】:

    在stackoverflow上找到了解决办法,稍微修改了一下。

    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);
    }
    

    【讨论】:

    • 感谢您格式化我的文章。我在哪里可以找到如何正确格式化我未来的问题?
    • 很好,但是如果文件是只读的,它将失败。您可能不想使用访问模式 FileAccess.Read 打开文件。
    • 请注意,当另一个线程在测试和读取之间打开文件时,ReadAllLines 仍然会失败。
    • 如果 FileAccess.Read 仍在写入文件,它会阻塞吗?
    【解决方案2】:

    使用DelayedFileSystemWatcher.cs 类http://blogs.msdn.com/b/ahamza/archive/2006/02/06/526222.aspx

    然后是这段代码。 检查PrintFileSystemEventHandler 事件处理程序。它尝试读取文件流中的文件,如果给出任何 ioerror,它假定文件仍在读取,因此它等待一段时间(在本例中为 2 秒)然后重试。检查CONVERSION: 标签

    static void Main(string[] args)
        {
            DelayedFileSystemWatcher dfw = new DelayedFileSystemWatcher(@"C:\\Test", "*.*");
            dfw.Changed += new FileSystemEventHandler(Program.FileSystemEventHandlerMethod);
            dfw.Created += new FileSystemEventHandler(Program.FileSystemEventHandlerMethod);
            dfw.Deleted += new FileSystemEventHandler(Program.FileSystemEventHandlerMethod);
            dfw.Error += new ErrorEventHandler(Program.ErrorEventHandlerMethod);
            dfw.Renamed += new RenamedEventHandler(Program.RenamedEventHandlerMethod);
    
            dfw.IncludeSubdirectories = true;
            dfw.ConsolidationInterval = 1000;
            dfw.EnableRaisingEvents = true;
            Console.WriteLine("Press \'q\' to quit the sample.");
            while (Console.Read() != 'q') ;
            //System.Threading.Thread.Sleep(60000);
            dfw.Dispose();
        }
        private static void FileSystemEventHandlerMethod(object sender, FileSystemEventArgs e)
        {
            PrintFileSystemEventHandler(e);
            System.Console.WriteLine();
        }
    
        private static void ErrorEventHandlerMethod(object sender, ErrorEventArgs e)
        {
            System.Console.WriteLine(e.GetException().Message);
            System.Console.WriteLine();
        }
    
        private static void RenamedEventHandlerMethod(object sender, RenamedEventArgs e)
        {
            PrintRenamedEventHandler(e);
            System.Console.WriteLine();
        }
    
        private static void PrintFileSystemEventHandler(FileSystemEventArgs e)
        {
    
        CONVERSION:
            try
            {
    
                if (e.ChangeType != WatcherChangeTypes.Deleted)
                {
                    if (!isFolder(e.FullPath) && isFile(e.FullPath))
                    {
                        FileStream fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.None);
                        fs.Close();
                    }
    
                }
                System.Console.WriteLine(e.Name + " " + e.FullPath + " " + e.ChangeType);
    
            }
            catch (System.IO.IOException)
            {
                Console.WriteLine("There was an IOException error or File is still copying. Retrying in 2 seconds...");
                System.Threading.Thread.Sleep(2000);
                goto CONVERSION;
            }
    
    
    
            //System.Console.WriteLine(e.Name);
            //System.Console.WriteLine(e.FullPath);
            //System.Console.WriteLine(e.ChangeType);
        }
    
        private static bool isFolder(string strPath)
        {
    
            bool isFolderExist = false;
            try
            {
                isFolderExist = Directory.Exists(strPath);
            }
            catch
            {
                isFolderExist = false;
            }
            return isFolderExist;
        }
    
        private static bool isFile(string strPath)
        {
    
            bool isFileExist = false;
            try
            {
                isFileExist = File.Exists(strPath);
            }
            catch
            {
                isFileExist = false;
            }
            return isFileExist;
        }
    
    
        private static void PrintRenamedEventHandler(RenamedEventArgs e)
        {
            PrintFileSystemEventHandler(e);
            System.Console.WriteLine(e.OldName);
            System.Console.WriteLine(e.OldFullPath);
        }
    

    我没有该项目的链接。但这会有所帮助。

    【讨论】:

    • 我可以在那里看到goto :)
    • 链接已损坏。
    【解决方案3】:

    我支持 Shay Erlichmen 接受的解决方案。 但无论如何 a)您可能不想使用访问模式 FileAccess.Read 打开文件,以防它是只读文件

    b) 某些程序在下载时,文件会有一些有趣的扩展名,完成后,扩展名会改变,虽然文件已经完成,但文件未找到异常。

    所以处理异常,同时订阅 file.renamed 事件

    【讨论】:

      【解决方案4】:

      AFAIK 复制完成后您不会收到通知,您可以实施重试机制。
      如果您遇到剪切违规,只需触发计时器以在 X 秒内重试操作。
      第二次重试应该在 X*2 秒之后等等(当然有一些限制)。

      【讨论】:

        【解决方案5】:

        如果可能的话,只需在您的fswCreated 中使用Thread.Sleep(500) 睡眠大约 1/2 秒。这应该会给您计算机完成写入文件所需的时间。

        当然,对于速度较慢的硬盘,这可能或可能足够的时间。

        【讨论】:

        • 如果文件非常大,比如 GB,这将不起作用。解决方案是尝试读取文件流中的文件并通过 Thread.Sleep 处理 io 异常,然后再次尝试读取文件检查我的以下答案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        • 1970-01-01
        • 2011-12-23
        • 2021-04-04
        相关资源
        最近更新 更多