在.NET中读取文件内容的变化 [英] Reading file content changes in .NET

查看:161
本文介绍了在.NET中读取文件内容的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中,大量的IPC是由附加到文件中1的过程,阅读从另一个进程中新的内容做了。

我想做的事情上面在Windows / .NET(太乱正常使用IPC,例如管道)。我追加到一个Python程序的文件,我想读的变化,只有每次FileSystemWatcher的报告事件的变化。我不想每次读取整个文件内容到内存中我在寻找变化(该文件将是巨大的)

每个追加操作会将一行数据,与一个独特的递增计数器(时间戳+键)开始,以换行符结束。

解决方案

 使用(的FileStream FS =新的FileStream
       (文件名,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
    {
        使用(StreamReader的SR =新的StreamReader(FS))
        {
            而(someCondition)
            {
                而(!sr.EndOfStream)
                    ProcessLinr(sr.ReadLine());
                而(sr.EndOfStream)
                    Thread.sleep代码(100);
                ProcessLinr(sr.ReadLine());
            }
        }
    }
 

这将帮助你只读追加行

In Linux, a lot of IPC is done by appending to a file in 1 process and reading the new content from another process.

I want to do the above in Windows/.NET (Too messy to use normal IPC such as pipes). I'm appending to a file from a Python process, and I want to read the changes and ONLY the changes each time FileSystemWatcher reports an event. I do not want to read the entire file content into memory each time I'm looking for changes (the file will be huge)

Each append operation appends a row of data that starts with a unique incrementing counter (timestamp+key) and ends with a newline.

解决方案

    using (FileStream fs = new FileStream
       (fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        using (StreamReader sr = new StreamReader(fs))
        {
            while (someCondition)
            {
                while (!sr.EndOfStream)
                    ProcessLinr(sr.ReadLine());
                while (sr.EndOfStream)
                    Thread.Sleep(100);
                ProcessLinr(sr.ReadLine());            
            }
        }
    }

this will help you read only appended lines

这篇关于在.NET中读取文件内容的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆