Click here to Skip to main content
Click here to Skip to main content

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.

Introduction

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.

Features

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);

   fileContentsControl.UpdateFileInfo(file);
   fileStatisticsControl.UpdateFileInfo(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.

History

  • 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.

License

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

Share

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 (http://www.unitedbinary.com [^]) 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 http://www.michaelckennedy.net [^]
 
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.

Comments and Discussions

 
GeneralLarge "busy" files appear to lock up GUI Pinmembercweaver4-Oct-10 15:06 
GeneralUse FileStream.Seek() for big files PinmemberRichard Redpay13-Mar-07 15:11 
GeneralFile Watcher for FAT File System [modified] Pinmemberalbert21426-Feb-07 21:27 
GeneralChange location of docked window PinmemberShakeel Mumtaz1-Oct-06 0:35 
Generalkeyword search and email options PinmemberJavaReb16-Aug-06 7:23 
GeneralGood tool! PinmemberJim Wakeen24-Apr-06 8:48 
QuestionSystem Watcher fails when file copy takes long time Pinmemberdarrenrhymer6-Oct-05 10:11 
Generala question PinmemberJ.L7-May-04 9:39 
GeneralRe: a question PinmemberMichael Kennedy29-Jun-04 17:44 
Questiongroup feature? Pinmemberchenhuisheng15-Apr-04 18:05 
AnswerRe: group feature? PinmemberMichael Kennedy19-Apr-04 20:44 
GeneralFrequently changed long files PinmemberLiborV8-Apr-04 6:20 
GeneralRe: Frequently changed long files PinmemberMichael Kennedy19-Apr-04 20:48 
GeneralIIS logs problem PinmemberYuri Gorobets1-Apr-04 6:03 
GeneralRe: IIS logs problem PinmemberMichael Kennedy1-Apr-04 15:28 
GeneralRe: IIS logs problem PinmemberBRI7028-Jun-05 2:29 
GeneralTracetool PinmemberThierry Parent1-Apr-04 5:02 
GeneralRe: Tracetool PinmemberMichael Kennedy1-Apr-04 15:23 
GeneralOpening the file Pinmembergarythom_work1-Apr-04 2:45 
GeneralRe: Opening the file PinmemberMichael Kennedy1-Apr-04 15:19 
GeneralRe: Opening the file PinmemberMichael Kennedy21-Apr-04 8:42 
GeneralThanks nice one Pinmemberhotgib1-Apr-04 1:56 
GeneralRe: Thanks nice one PinmemberMichael Kennedy1-Apr-04 15:13 
Hi Dennis,
 
Thanks, I'm glad you liked it. Those are some good suggestions. I especially like the tray icon support. Maybe it could use the notification balloons when a file changes. If I update the program I'll try to get that in there.
 
Take care,
Michael

 
Michael Kennedy
Partner, Software Engineer
United Binary, LLC
[^]

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 31 Dec 2004
Article Copyright 2004 by Michael Kennedy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid