Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

File Contents Watcher Application

Rate me:
Please Sign up or sign in to vote.
4.39/5 (20 votes)
30 Dec 20045 min read 148.6K   6.3K   92   23
A program for continuously monitoring and displaying text-based files.

Image 1

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

Image 2

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.

C#
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


Written By
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 Pin
cweaver4-Oct-10 15:06
cweaver4-Oct-10 15:06 
GeneralUse FileStream.Seek() for big files Pin
Digitalbeach13-Mar-07 15:11
Digitalbeach13-Mar-07 15:11 
GeneralFile Watcher for FAT File System [modified] Pin
albert21426-Feb-07 21:27
albert21426-Feb-07 21:27 
GeneralChange location of docked window Pin
Shakeel Mumtaz1-Oct-06 0:35
Shakeel Mumtaz1-Oct-06 0:35 
Generalkeyword search and email options Pin
shiftbit16-Aug-06 7:23
shiftbit16-Aug-06 7:23 
GeneralGood tool! Pin
Jim Wakeen24-Apr-06 8:48
Jim Wakeen24-Apr-06 8:48 
QuestionSystem Watcher fails when file copy takes long time Pin
darrenrhymer6-Oct-05 10:11
darrenrhymer6-Oct-05 10:11 
Generala question Pin
Member 599047-May-04 9:39
Member 599047-May-04 9:39 
GeneralRe: a question Pin
Michael Kennedy29-Jun-04 17:44
Michael Kennedy29-Jun-04 17:44 
Questiongroup feature? Pin
Huisheng Chen15-Apr-04 18:05
Huisheng Chen15-Apr-04 18:05 
AnswerRe: group feature? Pin
Michael Kennedy19-Apr-04 20:44
Michael Kennedy19-Apr-04 20:44 
GeneralFrequently changed long files Pin
LiborV8-Apr-04 6:20
LiborV8-Apr-04 6:20 
GeneralRe: Frequently changed long files Pin
Michael Kennedy19-Apr-04 20:48
Michael Kennedy19-Apr-04 20:48 
GeneralIIS logs problem Pin
Uri Gorobets1-Apr-04 6:03
Uri Gorobets1-Apr-04 6:03 
GeneralRe: IIS logs problem Pin
Michael Kennedy1-Apr-04 15:28
Michael Kennedy1-Apr-04 15:28 
GeneralRe: IIS logs problem Pin
BRI7028-Jun-05 2:29
BRI7028-Jun-05 2:29 
GeneralTracetool Pin
Thierry Parent1-Apr-04 5:02
Thierry Parent1-Apr-04 5:02 
GeneralRe: Tracetool Pin
Michael Kennedy1-Apr-04 15:23
Michael Kennedy1-Apr-04 15:23 
GeneralOpening the file Pin
garythom_work1-Apr-04 2:45
garythom_work1-Apr-04 2:45 
GeneralRe: Opening the file Pin
Michael Kennedy1-Apr-04 15:19
Michael Kennedy1-Apr-04 15:19 
GeneralRe: Opening the file Pin
Michael Kennedy21-Apr-04 8:42
Michael Kennedy21-Apr-04 8:42 
GeneralThanks nice one Pin
Dennis Battenfeld1-Apr-04 1:56
Dennis Battenfeld1-Apr-04 1:56 
GeneralRe: Thanks nice one Pin
Michael Kennedy1-Apr-04 15:13
Michael Kennedy1-Apr-04 15:13 

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.