The File Contents Watcher application allows you to load text-based files such as application or website log files and view them while they are being updated by your application or website. The application monitors the file for changes and automatically reloads the files if they are modified.
There are other applications available that display text-based files and automatically reload them when they are modified. An example of such a program is UltraEdit which I am currently using to write the first draft of this article. However, these programs are not written with the main purpose of monitoring a group of text files for changes and presenting them as a group. That's where the File Contents Watcher program comes in.
Below, I will present a few of features of this application and the concepts covered in developing this application.
The File Contents Watcher application lets you:
- Load one or more text-based files and view the contents in a textbox.
- Automatically reload the file and scroll the view to the end when the file is modified.
- List all the loaded files sorted by last modified time.
- View basic file properties such as size and modified time.
The feature set is pretty simple and doesn't require much elaboration. There are two features the might not be immediate clear when you run the program. The first is that you can double click a file in the All Files tab and that will jump you to that file's contents view. The other feature is that the contents view of the file is limited to 250,000 characters. This is to prevent huge text files from bogging down the program. Text files of any size can be loaded, but only the last 250,000 characters will be displayed. The reasoning for this is that most of the time the files will be modified on the end by appending new data.
Developing The Application
Developing the File Contents Watcher application was fairly simple. Basically, the application is a set of composite Windows Forms User Controls wrapped around
Warning: If you download the source code and want to view these user controls or the application's main form in the Windows Forms Designer, you must first compile the solution or the controls will not show up.
The application is made up of the following User Controls:
- All Files Control - Maintains the list of loaded files and provides file navigation.
- File Control - Displays information and contents of a file.
- File Contents Control - Used by the File Control to display the contents of a file.
- File Statistics Control - Used by the File Control to display the file properties.
This diagram shows how the controls are used in the application. Note that the lines between controls mean the top control "contains" or "is composed of" the lower control.
User Interface Control Composition Diagram
The User Controls are contained in a Magic Library
TabControl. See the DotNetMagic User Controls section at the end of this article for details about this control library.
Watching The Files
FileSystemWatcher class is central to the operation of this program. The following method is from the
FileControl class and is called once when that control is created.
public void Initialize(string file)
labelFileName.Text = file;
fileSystemWatcher.Filter = System.IO.Path.GetFileName(file);
fileSystemWatcher.Path = System.IO.Path.GetDirectoryName(file);
The method initializes the
fileSystemWatcher object with the correct path and file name. Since the
fileSystemWatcher object was added through the Windows Forms designer, this is all that it takes to start it watching the specified file. In the designer, the
FileControl has signed up for the
FileSystemWatcher.Changed event which will be fired when the specified file changes. The
FileControl signals the other controls and sub-controls to update themselves when it receives a
This application is something I started for myself and I only wanted to spend a couple of hours making it. So the feature set isn't as polished as it could be. For example, the main form and controls could remember their location and state. There could be a recent file list.
I may implement one or more of these features if there is enough interest. For now, I'm releasing the application in a fairly bare-bones state. Hope you enjoy it.
DotNetMagic User Controls
The application uses the Magic Library
TabControl for organizing the various controls in the user interface. The
TabControl is one of the controls in the excellent Magic Library from Phil Wright [^]. I've used these in several applications, and they are very solid controls and I certainly recommend them.
The version used with this application is from the Magic Controls library Phil posted to Code Project with his article Magic TabControl - VS.NET Style [^].
He has since turned the Magic Library into a commercial User Controls library called DotNetMagic.
- December 29, 2004:
- Changed the load log file dialog from single file mode to multiple file mode. You can now load many log files at once.
- Removed "Save File Group" confirmation when program closes and only one file is loaded.
- Removed "Save File Group" confirmation when program closes because Windows is exiting.
- Added word-wrap support. This can be toggled using the View menu and applies to all loaded files.
- Changed the display font to a fixed-length font (Courier New).
- Changed hyperlinks to load using the default browser rather than hard coding Internet Explorer.
- April 7, 2004:
- Added the ability to save and load file groups to more easily manage groups of files.
- View is now scrolled to the end of files when the file is initially loaded.
- Fixed broken link for Magic Controls in the article HTML.
- March 31, 2004:
- This is the initial release of the article. No changes.