1) Create the FileSystemWatcher and set its properties.
FileSystemWatcher_Log_1.Path = Directory.GetParent("C:\\temp").FullName;
FileSystemWatcher_Log_1.Filter = "MyLogFile.log";
NotifyFilters notifyFileters = new NotifyFilters();
notifyFileters = notifyFileters
NotifyFilters.LastWrite;
// You can choose other notification filters
FileSystemWatcher_Log_1.NotifyFilter = notifyFileters;
FileSystemWatcher_Log_1.EnableRaisingEvents = true;
2) Set the event handler.
this.FileSystemWatcher_Log_1.Changed += new
System.IO.FileSystemEventHandler(this.FileSystemWatcher_Changed);
3) Update GUI when event occours
// create stream writer only once so that whenever new log is appended in the log file,
// only that text will be shown in the list view (or which ever the gui component you are using).
if (null == sr_Log_1)
{
sr_Log_1 = new StreamReader(new FileStream(SelectedFileName_Log1_textbox.Text,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite), true);
}
string line;
while (sr_Log_1.EndOfStream != true)
{
line = sr_Log_1.ReadLine();
if (line.Trim().Length == 0)
{
continue;
}
Log_1_listBox.Items.Add(line);
Log_1_listBox.TopIndex = Log_1_listBox.Items.Count - 26 + 1;
}
FileSystemWatcher_Log_1.Path = Directory.GetParent("C:\\temp").FullName;
FileSystemWatcher_Log_1.Filter = "MyLogFile.log";
NotifyFilters notifyFileters = new NotifyFilters();
notifyFileters = notifyFileters
NotifyFilters.LastWrite;
// You can choose other notification filters
FileSystemWatcher_Log_1.NotifyFilter = notifyFileters;
FileSystemWatcher_Log_1.EnableRaisingEvents = true;
2) Set the event handler.
this.FileSystemWatcher_Log_1.Changed += new
System.IO.FileSystemEventHandler(this.FileSystemWatcher_Changed);
3) Update GUI when event occours
// create stream writer only once so that whenever new log is appended in the log file,
// only that text will be shown in the list view (or which ever the gui component you are using).
if (null == sr_Log_1)
{
sr_Log_1 = new StreamReader(new FileStream(SelectedFileName_Log1_textbox.Text,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite), true);
}
string line;
while (sr_Log_1.EndOfStream != true)
{
line = sr_Log_1.ReadLine();
if (line.Trim().Length == 0)
{
continue;
}
Log_1_listBox.Items.Add(line);
Log_1_listBox.TopIndex = Log_1_listBox.Items.Count - 26 + 1;
}
No comments:
Post a Comment