2

I have a text file which is getting updated every time from the server data .Now as per my requirement i have to read this file line by line continuously.I know how to read a file line by line but not getting how to read it continuously.Here is my c# code to read file line by line...

if (System.IO.File.Exists(FileToCopy) == true)
        {

            using (StreamReader reader = new StreamReader(FileToCopy))
            {
                string line;
                string rawcdr;

                while ((line = reader.ReadLine()) != null)
                {
                   //Do Processing
                }
              }
         }

As per my requirement i have to watch a text file continuously for changes.Suppose a new line has been added into the text file,the moment it has added it should be read by above defined code and processing should be performed as per the conditions.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Adi
  • 1,395
  • 11
  • 37
  • 61
  • For reference, the UNIX utility [`tail -f`](http://stackoverflow.com/questions/1439799/how-can-i-get-the-source-code-for-the-linux-utility-tail) implements this. They call it "following" a file. – Jonathon Reinhart Jan 29 '14 at 05:13
  • possible duplicate of [c# continuously read file](http://stackoverflow.com/questions/3791103/c-sharp-continuously-read-file) – Håkan Fahlstedt Jan 29 '14 at 05:14

1 Answers1

6

You can use FileSystemWatcher to listens to the file system change notifications and raises events when a directory, or file in a directory. If the text is appended in the text file but not modified then you can keep track of line numbers you have read and continue after that when change event is triggered.

private int ReadLinesCount = 0;
public static void RunWatcher()
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = "c:\folder";   
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;   
    watcher.Filter = "*.txt";    
    watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.EnableRaisingEvents = true;

}

private static void OnChanged(object source, FileSystemEventArgs e)
{
      int totalLines - File.ReadLines(path).Count();
      int newLinesCount = totalLines - ReadLinesCount;
      File.ReadLines(path).Skip(ReadLinesCount).Take(newLinesCount );
      ReadLinesCount = totalLines;
}
Adil
  • 146,340
  • 25
  • 209
  • 204
  • You have to read file on change event, every time the file changed you will get the event. I have given code for binding the event and reading lines, you have to give the read lines and lined you need to read in skip and take methods. – Adil Jan 29 '14 at 05:31
  • 1
    NewLinesCount would be zero on start, then you have to update it each time you read the lines. It is like bookmark. NewLinesCount would be total lines count - NewLinesCount – Adil Jan 29 '14 at 06:07
  • what about ReadLinesCount? – Adi Jan 29 '14 at 06:12
  • 1
    Declare it as a data member and initialize it with zero, check my updated code – Adil Jan 29 '14 at 06:17
  • Getting "The directory name D:\BEML.txt is invalid" in line watcher.Path = "D:\\BEML.txt"; – Adi Jan 29 '14 at 06:24
  • 1
    You have to give the folder path, I guess, put file in some folder and give path of that folder. – Adil Jan 29 '14 at 06:27
  • How to debugg this program by manually updating text file in visual studio ? – Adi Jan 29 '14 at 07:55