Introduction
With the speed with which technology moves ahead and companies release new versions of their programming languages and frameworks, it has been too difficult to focus on everything in a platform and be a master of everything. This expansion in technology has one more consequence and that is, you will find everything about new technologies since the developers are focusing more on them. But if you want to develop a program with some basic system level functionalities, you may find it difficult to find code snippets or tools to shorten your development time or even guidelines about how to do what you want.
But no matter how fast development platforms expand, the necessity of using basic classes or programming approaches is never lost. One class amongst these classes which is very interesting to use is the FileSystemWatcher
class. In this article, I am not going to teach you how to use this class. You can find a lot of articles on the Internet explaining how to use this class or the kind of errors people get while using this class.
Background
In this article, I am going to show you my solution to a very simple problem that you may need to solve. I hope that if you have the same problem, this code snippet can be useful to you.
I have a client server application communicating with each other using port 2000. The client application gets the data from Server and processes it and at the same time it writes the log to the file for debugging purpose. Last week I got a new requirement to develop an application to monitor the log file and get the latest change from the last change and process it with some business logic. For example if AAAA; BBBB; CCCC (where “;” is a separator between different packets) is in the file and CCCC is the latest packet received from server, my new application should read CCCC and process it.
Using the Code
The solution I came up with is to use a temporary file that acts as a container for the latest data. Whenever log is added to the log file, I read the complete log file and temp files and compare them. After getting the difference which is the new packet, I write the changes to the temp file so that both log file and change file be exactly the same so that next time I can compare the temp file with log file. It is obvious that after getting the data and logging it into log file, size of the log file is bigger than the temp file and I can compare and get the difference.
static void Main ( string [ ] args )
{
string path = @"c:\Test";
FileSystemWatcher watch = new FileSystemWatcher ( path , "WatchTest.Log" );
watch.Changed += new FileSystemEventHandler ( watch_Changed );
watch.EnableRaisingEvents = true;
Console.ReadLine( );
}
void watch_Changed ( object sender , FileSystemEventArgs e )
{
string tempPath = System.Environment.GetEnvironmentVariables ( )
[ "TEMP" ].ToString ( )+"\\Log.temp";
if ( !File.Exists ( tempPath ) )
{
File.Create( tempPath );
}
string temp = File.ReadAllText( tempPath );
string difference = string.Empty;
string path = e.FullPath;
StreamReader reader = new StreamReader( path );
string content = reader.ReadToEnd( );
reader.Close( );
if ( content.Length > temp.Length )
{
if ( temp.Length > 0 )
{
difference = content.Replace( temp , "" );
}
else
{
difference = content;
}
File.WriteAllText( tempPath , content );
}
else
{
File.WriteAllText( tempPath , content );
}
}
References
History
- 4th December, 2008: Initial post
This is version 1.0 of this code snippet. I will be updating this if I learn about what else can be added to this snippet to make it more useful.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.