Click here to Skip to main content
13,597,671 members
Click here to Skip to main content
Add your own
alternative version


91 bookmarked
Posted 31 Mar 2004

File Contents Watcher Application

, 30 Dec 2004
Rate this:
Please Sign up or sign in to vote.
A program for continuously monitoring and displaying text-based files.


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:

  1. Load one or more text-based files and view the contents in a textbox.
  2. Automatically reload the file and scroll the view to the end when the file is modified.
  3. List all the loaded files sorted by last modified time.
  4. 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 FileSystemWatcher objects.

User Controls

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

The 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 Changed event.

Future Work

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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Michael Kennedy
Instructor / Trainer DevelopMentor
United States United States
Michael Kennedy is a founding partner and software engineer at United Binary, LLC ( [^]) and he is active in the agile software development community. Michael has been developing software for over 10 years. The last 4 of those years have been solidly focused on .NET development. For more information, please visit his website [^]

In a previous life, Michael was pursuing a fairly successful career in mathematics before he saw the True Light and chose The Way of Programming.

You may also be interested in...


Comments and Discussions

Generala question Pin
J.L7-May-04 9:39
memberJ.L7-May-04 9:39 
GeneralRe: a question Pin
Michael Kennedy29-Jun-04 17:44
memberMichael Kennedy29-Jun-04 17:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180621.3 | Last Updated 31 Dec 2004
Article Copyright 2004 by Michael Kennedy
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid