Reputation: 1455
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)
{
}
}
}
Upvotes: 0
Views: 834
Reputation: 653
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.
Upvotes: 1