?
Solved

How to monitor changes in file content?

Posted on 2008-02-12
7
Medium Priority
?
1,278 Views
Last Modified: 2013-12-17
Hi,

I need to write a simple(?) component that will monitor the changes being made to a text file and notify them to registered listeners.

I have a process which writes data to a text file in the following way:

Start
... //data
End
Start
...//data
End
...
etc

I want to monitor the file and get notified when certain lines are written (e.g. Start/End).

A quick google search yielded the FileSystemWatcher class but I don't think it fits since it only monitors file system events and not file content, but I could be wrong as I'm fairly new to C#/.NET.

Thanks for any help.

0
Comment
Question by:ajny
  • 3
  • 2
  • 2
7 Comments
 
LVL 5

Accepted Solution

by:
Colemss earned 225 total points
ID: 20875492
Something like this..  Then when the changed event fires look in the file for the changes.

You will probably need to keep counter for start and one for end then when the file changes read the file and compare the counts.


static void Main(string[] args)
        {
            FileSystemWatcher watch = new FileSystemWatcher();
            watch.Path = @"D:\tmp";

            watch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            // Only watch text files.
            watch.Filter = "*.txt";

            watch.Changed += new FileSystemEventHandler(OnChanged);
            watch.Created += new FileSystemEventHandler(OnChanged);
            watch.Deleted += new FileSystemEventHandler(OnChanged);
            watch.Renamed += new RenamedEventHandler(OnRenamed);

            watch.EnableRaisingEvents = true;

            Console.ReadLine();
        }

        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            if(e.FullPath == @"D:\tmp\p.txt")
                Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);

        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            // Specify what is done when a file is renamed.
            if (e.FullPath == @"D:\tmp\p.txt")
                Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
        }
0
 

Author Comment

by:ajny
ID: 20876399
Hi Colemss,

Thanks for your quick reply!

I was hoping there's a better solution.

As I wrote, I'm already aware of the FileSystemWatcher class and its usages.
What I'm trying to write is a generic file watcher that can detect an arbitrary pattern (regex) being written to a file.

Using FileSystemWatcher will force me to re-read the file every time or (more likely) to keep track of where I stopped the last time and seek to that location before the next search.
Although these approaches will work, I was hoping there's a way to hook the monitor on the write operation (in a sense like a filter), so it can "see" what's about to be written to the file.
0
 
LVL 5

Expert Comment

by:Colemss
ID: 20877047
The only way I can imagine doing this would be with some sort of system hook.  It would require win32 dll hooks.  Most likely you will find c++ examples.  These would be undocumented Microsoft core hooks.  Unless this is  a major deal its probably not worth doing.  I have never seen any examples of looking at the data before or during a file write. It would be interesting.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 6

Assisted Solution

by:monarch_ilhan
monarch_ilhan earned 225 total points
ID: 20879021
You can create a  unix tail function equivalent  in c# there is already one done.
By a  FileSystemWatcher object you may check your file and determine any changes, check whether it contains Start/End blocks by Tailer class.

If you interested, check this link.
http://www.codeproject.com/KB/cs/wintail.aspx?df=100&forumid=31437&exp=0&select=1183475
0
 
LVL 5

Expert Comment

by:Colemss
ID: 20879075
That is the full source to my first suggestion.  I would try it. But it is using FileSystemWatcher.
0
 
LVL 6

Expert Comment

by:monarch_ilhan
ID: 20879132
He has to use a timer or a filesystemwatcher to monitor changes. I believe a filesystemwatcher will have a better performance.
0
 

Author Comment

by:ajny
ID: 20883092
Thanks for your help, guys.

I'll wait a little while to see if someone can come up with a better solution before I resort to FileSystemWatcher.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question