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

Tail utility for windows

Rate me:
Please Sign up or sign in to vote.
4.69/5 (15 votes)
18 Jan 20042 min read 146.1K   3.9K   38   20
Tail utility for windows

Introduction

This simple program imitates the unix "tail -f" utility on windows.

Source code and Demo

The submitted source code contains one main file: Tail.cs. This is the core of the tail program. This file is used in two wrappers - a GUI wrapper and a CUI wrapper. The two projects in the solution illustrate using Tail.cs in a windows form application and a console application.

Code Walkthrough

.NET introduced the FileSystemWatcher, which is the main component of this program. By configuring this component one can listen to file system changes.

I first check whether the file exists. If it does exist then I monitor any changes to the file thus:

C#
fileSystemWatcher.Changed += new FileSystemEventHandler(TargetFile_Changed);

If it does not exist yet, then I wait until the file is created before watching for file change notifications:

C#
fileSystemWatcher.Created += new FileSystemEventHandler(TargetFile_Created);

When a change is made to the file, the method below gets the notification

C#
public void TargetFile_Changed(object source, FileSystemEventArgs e)
{
 lock (this)
 {
  byte[] bytesRead = new byte[maxBytes];
  FileStream fs = new FileStream(this.filename, FileMode.Open);
  if (fs.Length > maxBytes)
  {
   this.previousSeekPosition = fs.Length - maxBytes;
  }
  this.previousSeekPosition = (int)fs.Seek(
      this.previousSeekPosition, SeekOrigin.Begin);
  int numBytes = fs.Read(bytesRead, 0, maxBytes);
  fs.Close();
  this.previousSeekPosition += numBytes;

  StringBuilder sb = new StringBuilder();
  for (int i=0; i<numBytes; i++)
  {
   sb.Append((char)bytesRead[i]);
  }

  this.MoreData(this, sb.ToString());
 }
}

Within this, I ensure that the last characters are read in a large file which is longer than the number of characters that can be read. I then read the last set of characters starting from where we left off the previous time. The string of new characters is then sent to all listeners.

The sample programs listen to this notfication and then updates its user interface.

Issues

I have noticed an occasional exception saying that the target file is being accessed by another program. In the environment that I am working in, the target file is not always opened for writing, so I am able to get away with the above code for now. If it works, don't touch it uh. :-) But you feel free to correct it and post changes.

Bug Fixes

  • Added fix for file access exception. Thanks to Gary a.k.a garythom_work for this fix. (see thread below) - 17 Jan 2004

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
France France
~/sathishvj

Comments and Discussions

 
QuestionCross-thread operation not valid Pin
TinyTony_16-Oct-13 0:05
TinyTony_16-Oct-13 0:05 
BugWell, this definitely does not work Pin
Zacky Pickholz20-Nov-12 8:05
Zacky Pickholz20-Nov-12 8:05 
GeneralSlower than native code... Pin
therearefartoomanybens10-Mar-09 11:56
therearefartoomanybens10-Mar-09 11:56 
GeneralRe: Slower than native code... Pin
Lord of Scripts24-Sep-09 1:14
Lord of Scripts24-Sep-09 1:14 
GeneralNice little utility Pin
rc_cohn10-Nov-05 12:11
rc_cohn10-Nov-05 12:11 
GeneralSome text editors does not trigger fileSystemWatcher.Changed event! Pin
Tomer Shalev3-Aug-05 7:32
Tomer Shalev3-Aug-05 7:32 
GeneralRe: Some text editors does not trigger fileSystemWatcher.Changed event! Pin
meraydin2-Dec-05 6:51
meraydin2-Dec-05 6:51 
GeneralUse in threads Pin
bollwerj24-May-04 8:11
bollwerj24-May-04 8:11 
GeneralRe: Use in threads Pin
Lord of Scripts24-Sep-09 1:36
Lord of Scripts24-Sep-09 1:36 
GeneralCall to TargetFile_Changed Pin
bollwerj28-Apr-04 9:41
bollwerj28-Apr-04 9:41 
Hi
First I'd like to say thank you very much for this article. I'm fairly new to .Net and had not come across either the FileSystemWatcher class or the StringBuilder class so this article was very beneficial in that respect.

I don't speak C#, I work in VC++ so I converted your code and to VC++ and customized it a bit to fit into a VC++ GUI that I'm building. (Made for a good learning experience.) I'm happy to say it works like a champ for what I need it to do.

I think I understand most of what is going on with the exception of the call to TargetFile_Changed(NULL< NULL) that is made during initialization. I guessing that the purpose of that call is to simply pick up anything that may currently exist in the file. Is that correct?

One last thing, when converting the Byte array data to StreamBuilder data, in VC++, a cast to char doesn't work. It produced decimal numbers instead of the character equivilants. I had to use the Convert::ToChar() to get characters instead of the decimal codes.

Thanks again for the project, it makes for a good tutorial as well as a useful tool.

Cheers

John Bollwerk
Smile | :)


GeneralRe: Call to TargetFile_Changed Pin
SathishVJ28-Apr-04 18:38
SathishVJ28-Apr-04 18:38 
GeneralRe: Call to TargetFile_Changed Pin
bollwerj30-Apr-04 3:55
bollwerj30-Apr-04 3:55 
GeneralRe: Call to TargetFile_Changed Pin
bollwerj3-May-04 2:38
bollwerj3-May-04 2:38 
GeneralAn Alternative Pin
Gary Brewer14-Feb-04 5:13
Gary Brewer14-Feb-04 5:13 
GeneralTraceTool and Tail windows Pin
Thierry Parent19-Jan-04 20:59
Thierry Parent19-Jan-04 20:59 
GeneralFree SFU 3.5 Pin
Anonymous19-Jan-04 7:40
Anonymous19-Jan-04 7:40 
Questionwhat does tail -f do? Pin
peterchen19-Jan-04 1:46
peterchen19-Jan-04 1:46 
AnswerRe: what does tail -f do? Pin
SathishVJ20-Jan-04 3:34
SathishVJ20-Jan-04 3:34 
GeneralThows an Exception Pin
garythom_work15-Jan-04 5:03
garythom_work15-Jan-04 5:03 
GeneralRe: Thows an Exception Pin
SathishVJ20-Jan-04 3:27
SathishVJ20-Jan-04 3:27 

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.