0

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. 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?

0

2 Answers 2

3

You can use the 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.

private static void OnChanged(object source, FileSystemEventArgs e)
{
   if(e.ChangeType == WatcherChangeTypes.Changed)
   {
       string lastLine = File.ReadLines(e.FullPath).Last();
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

private void OnChanged(object source, FileSystemEventArgs e) { if (e.ChangeType == WatcherChangeTypes.Changed) { string lastLine = File.ReadLines(e.@"C:\Textdokument.txt").Last(); } }
The Path is underlined and it says Identifier expected.
OK, I think I've got it now! Ill write when I am ready... Thank you!
0

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.

And then use below code: to only read the last line

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

3 Comments

using (var md5 = new MD5CryptoServiceProvider()) { var buffer = md5.ComputeHash(ReadAllBytes(filename)); var sb = new StringBuilder(); for (var i = 0; i < buffer.Length; i++) { sb.Append(buffer[i].ToString("x2")); } return sb.ToString(); }
The above code is for calculating the MD5 hash of the file and comparison
This answer is incomplete - you're not showing the code for your ReverseLineReader class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.