0

I have a method which contains FileSystemWatcher to watch any changes in the text file.Here is my method.

public static void RunWatcher()
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = "D:\\CDR File";
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    watcher.Filter = "*.txt";
    watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.EnableRaisingEvents = true;
}

And here is my method which is called from RunWatcher() method..

private static void OnChanged(object source, FileSystemEventArgs e)
{
    int totalLines = File.ReadLines(FileToCopy).Count();
    int newLinesCount = totalLines - ReadLinesCount;
    File.ReadLines(FileToCopy).Skip(ReadLinesCount).Take(newLinesCount);
    ReadLinesCount = totalLines;

    Console.WriteLine("Hello World");
    Console.ReadLine();
}

Now i have called RunWatcher() method in the main method of the application and put a breakpoint inside RunWatcher() method.But on debugging i am not able to call OnChanged ..what is the problem ?why is it not getting debugged and hitting the braekpoint?

Here is what i have tried as per suggestion from Hans Passant

        string FileToCopy = "D:\\BEML.txt";

        if (System.IO.File.Exists(FileToCopy) == true)
        {
            var fs = new FileStream(FileToCopy, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            using (var reader = new StreamReader(fs))
            {
                string line;
                string rawcdr;

                while ((line = reader.ReadLine()) != null)
                {
                }
            }
          }
Adi
  • 1,395
  • 11
  • 37
  • 61
  • Set the breakpoint on OnChanged of course. The code is very unwise, you do *not* want to use FSW to measure copy progress like this. You get progress from [CopyFileEx](http://msdn.microsoft.com/en-us/magazine/cc163851.aspx). Hopefully your D: drive is not an optical drive, that cannot work. – Hans Passant Jan 29 '14 at 08:36
  • @HansPassant FileSystemWatcher is to continuous check updates in the text file – Adi Jan 29 '14 at 08:38
  • 1
    You are reading the entire file *twice* for each change notification. As written, that's murderously expensive and the odds that you'll lose change events are *very* high. Always use the Error event. Get ahead by opening the file with FileShare.ReadWrite and not closing it so you don't have to keep reading it over and over again from the start. Do keep in mind that you can't reliably detect end-of-file while the file is being written, the last line is likely to be a partially written line of text. You'll count that as a line, the next write counts it as a line again. – Hans Passant Jan 29 '14 at 08:46
  • @HansPassant sorry sir but i am not getting how to use FileShare.ReadWrite to use read text file continuously.Please help me.. – Adi Jan 29 '14 at 09:38
  • http://stackoverflow.com/a/4964737/17034 – Hans Passant Jan 29 '14 at 09:39
  • @HansPassant i have updated my post with your suggestion .Please tell me if it is the correct to read a text file line by line continuously. – Adi Jan 29 '14 at 09:50

1 Answers1

1

OnChanged is not called from RunWatcher. The calling of eventhandlers are handled in the background by the runtime. So you need to set the breakpoint in OnChanged.

j.karlsson
  • 618
  • 4
  • 14
  • I checked setting breakpoint in OnChanged and changed the file ..stil i am not seeing the output in Console ... – Adi Jan 29 '14 at 08:31
  • 1
    Have you built everything with debugging information? Without it you will not be able to set the breakpoint. – j.karlsson Jan 29 '14 at 08:39
  • yes i have build the solution in debugging mode..is my code correct? – Adi Jan 29 '14 at 08:43