简体   繁体   English

使用C#.Net检查文本文件中的新行

[英]Checking for a new line in Text file using C# .Net

Could someone tell me what is the best way to check for a new Line in a Text file? 谁能告诉我检查文本文件中新行的最佳方法是什么? I have a scale, it writes weight values into a text file. 我有一个秤,它将体重值写入文本文件。 Then my C# program has to read out the last value und use it for other calculation. 然后,我的C#程序必须读出最后一个值,并将其用于其他计算。 I would read the last line of the text and repeat this after few seconds, but this is a bad solution because it would be better if I could read the last value only then when a new value has been written to file, not every few seconds. 我将阅读文本的最后一行,并在几秒钟后重复此操作,但这是一个糟糕的解决方案,因为如果只有在将新值写入文件时才读取最后一个值,而不是每隔几秒钟,这样会更好。 So how do I check for a new line in a file? 那么,如何检查文件中的新行? Should I use the file date or file size? 我应该使用文件日期或文件大小吗? Whats the best way? 最好的方法是什么? Hope someone can help. 希望有人能帮忙。

       while (checkBox1.Checked)
        {
            //Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = @"C:\";

            /* Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            // Only watch text files.
            watcher.Filter = @"Textdokument.txt";
            watcher.Changed += new FileSystemEventHandler(OnChanged);
        }


    }

    private void OnChanged(object source, FileSystemEventArgs e)
    {
         textBox1.Text = "1234";


        if (e.ChangeType == WatcherChangeTypes.Changed)
        {

            string lastLine = File.ReadLines(e.FullPath).Last();
            textBox1.Text = lastLine;
        }
    }

This is my code until now. 到目前为止,这是我的代码。 No Errors are shown but still nothing happens. 没有显示错误,但仍然没有任何反应。 Could it be that the problem is that the other application is writing into the file while I want to read from it? 可能是我想从文件中读取文件时,另一个应用程序正在将文件写入文件吗? It shouldnt be a problem, or am I wrong? 这不应该是一个问题,否则我错了吗?

You can use the FileSystemWatcher 您可以使用FileSystemWatcher

The Changed event is raised when changes are made to the size, system attributes, last write time, last access time, or security permissions of a file or directory in the directory being monitored. 对受监视目录中的文件或目录的大小,系统属性,上次写入时间,上次访问时间或安全权限进行Changed时,将引发Changed事件。

private static void OnChanged(object source, FileSystemEventArgs e)
{
   if(e.ChangeType == WatcherChangeTypes.Changed)
   {
       string lastLine = File.ReadLines(e.FullPath).Last();
   }
}

You can try to calculate the MD5 hash of the file and compare to the original MD5 hash and if these two don't match the file was modified. 您可以尝试计算文件的MD5哈希值,然后将其与原始MD5哈希值进行比较,如果这两个哈希值不匹配,则文件已被修改。

And then use below code: to only read the last line 然后使用下面的代码:只读取最后一行

var lines = new ReverseLineReader(filename); var last = lines.Take(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM