IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Tutorial: How to watch a directory for events using C#

3 posters

Go down

Tutorial: How to watch a directory for events using C#  Empty Tutorial: How to watch a directory for events using C#

Post by BIT0122-Amit Thu Apr 14, 2011 5:02 am

Intended Audience:This tutorial requires decent knowledge about programming in C#.
Difficulty: Beginner

Sometimes, you need to "watch" a folder for events like inserting a file/directory, changing them, or deleting them..Net
provides a simple way(FileSystemWatcher) of doing so. It watches a folder for events. When an event(that is, change) is thrown(occurs) FileSystemWatcher catches(notices) it. Here is a sample code of doing it. I think the code itself is pretty much self explanatory. If you are still confused, you should hover your mouse over the functions to see
their description. Or you can ask here.

Here is the code:

Code:
using System;
using System.IO;

namespace DirectoryWatch {

    class Watcher {
        FileSystemWatcher fileWatcherIIT;

        public Watcher() {
            //Create a Filesystem watcher object
            fileWatcherIIT = new FileSystemWatcher("D:\\exp\\watchThis", "*.*");
            //allow the watcher to raise events when any modifications
            //are made to watched folder
            fileWatcherIIT.EnableRaisingEvents = true;
           
            fileWatcherIIT.IncludeSubdirectories = true;
            //notice all sorts of changes. Other types of changes are
            //changed, deleted, renamed, created
            fileWatcherIIT.WaitForChanged(WatcherChangeTypes.All);
            /*
            * += is a special expression. It means we are adding a
            * File System event handler or watcher. Notice the things in bracket?
            * they are static function names. The functions are created below*/
            fileWatcherIIT.Changed += new FileSystemEventHandler(OnChanged);
            fileWatcherIIT.Created += new FileSystemEventHandler(OnCreated);
            fileWatcherIIT.Deleted += new FileSystemEventHandler(fileWatcherIIT_Deleted);
            fileWatcherIIT.Renamed += new RenamedEventHandler(fileWatcherIIT_Renamed);
        }
        /*
        * self explanatory function I think. although the sender object is
        * not exactly used here, but it is the required parameter
        *
        */
        void fileWatcherIIT_Renamed(object sender, RenamedEventArgs e) {
            Console.WriteLine(
                "Change noticed: Object Name = {0}, Object Event: {1} File Content",
                e.Name, e.ChangeType);
        }

        public static void fileWatcherIIT_Deleted(object sender, FileSystemEventArgs e) {
            Console.WriteLine(
                "Change noticed: Object Name = {0}, Object Event: {1} File Content",
                e.Name, e.ChangeType);
        }

        public static void OnChanged(object source, FileSystemEventArgs fileSystemEvent) {
            Console.WriteLine(
                "Change noticed: Object Name = {0}, Object Event: {1} File Content",
                fileSystemEvent.Name, fileSystemEvent.ChangeType);
        }
        public static void OnCreated(object source, FileSystemEventArgs fileSystemEvent) {
            Console.WriteLine(
                "Change noticed: Object Name = {0}, Object Event: {1}",
                fileSystemEvent.Name, fileSystemEvent.ChangeType);
        }
    }
}


Feed backs are welcome Smile
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Tutorial: How to watch a directory for events using C#  Empty Re: Tutorial: How to watch a directory for events using C#

Post by BIT0130-Shakkhor Fri Apr 15, 2011 12:27 am

This is a nice fragment man. Rep++
BIT0130-Shakkhor
BIT0130-Shakkhor
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 209
Points : 328

Back to top Go down

Tutorial: How to watch a directory for events using C#  Empty Re: Tutorial: How to watch a directory for events using C#

Post by BIT0122-Amit Sat Jun 11, 2011 10:33 pm

I think the moderators of this section are supposed to take a look at tutorials, then if necessary, provide feedback, and then if qualified, move tutorials to the Tutorials section.

But null reply is a bit mystifying Tutorial: How to watch a directory for events using C#  221617

This topic has been here for a bit more than a month. Good enough time to take a look I think. Wink

Or paying respect to rules decided by everyone instead of forcing this tutorial to the tutorials zone was a mistake?
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Tutorial: How to watch a directory for events using C#  Empty Re: Tutorial: How to watch a directory for events using C#

Post by BIT0130-Shakkhor Tue Jun 28, 2011 10:22 pm

I've used this fragment in my Printer Driver. salute
BIT0130-Shakkhor
BIT0130-Shakkhor
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 209
Points : 328

Back to top Go down

Tutorial: How to watch a directory for events using C#  Empty Re: Tutorial: How to watch a directory for events using C#

Post by BIT0122-Amit Tue Jun 28, 2011 11:21 pm

Glad to know.
And yet, this is not accepted as a tutorial by the honorable moderators Smile
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Tutorial: How to watch a directory for events using C#  Empty Re: Tutorial: How to watch a directory for events using C#

Post by BIT0112-Rokon Sat Aug 06, 2011 4:20 am

good work kiddo, at last I got some time to watch it.

I think, its not a tutorial, its more likely tips and tricks. right?

now my suggestion, make a library and be careful while writing codes. Try to use proper encapsulation. A class without encapsulation is more likely a pornography.

you could write :

private FileSystemWatcher fileWatcherIIT;



Good work and rep++
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Tutorial: How to watch a directory for events using C#  Empty Re: Tutorial: How to watch a directory for events using C#

Post by BIT0122-Amit Sat Aug 06, 2011 4:34 am

First of all, I am honored Smile Thank you for putting this in the tutorials section.

In my humble opinion, a tips, or tricks would be like

"Hey! You can use this to do that!"

Explaining things which is specially prepared for teaching another person something, is tutorial.

I am showing how to use a library Neutral So, erm.. you meant make a library which explains using a library?

About accessibility,

The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Tutorial: How to watch a directory for events using C#  Empty Re: Tutorial: How to watch a directory for events using C#

Post by BIT0112-Rokon Sat Aug 06, 2011 4:51 am

I mean, write a library that can be plugged in some other place. or write a utility software.

let me share my idea.
for say, C:\\some_folder
I want to watch it.
simply write a peach of code, that will watch the folder from the start-up and write down in a text file as a report.
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Tutorial: How to watch a directory for events using C#  Empty Re: Tutorial: How to watch a directory for events using C#

Post by BIT0122-Amit Sat Aug 06, 2011 5:02 am

Interesting idea.
However, when I wrote this I thought I should write something that will give its readers a huge scope of doing things. For example, I originally used this to watch folders so that it can upload automatically to another place. Shakkhor used this in his software for another reason. What I mean is, after reading this, people know how to watch a folder for changes. How is he going to do this, is up to him.

But your idea, is interesting as I mentioned. Very Happy
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Tutorial: How to watch a directory for events using C#  Empty Re: Tutorial: How to watch a directory for events using C#

Post by BIT0130-Shakkhor Sat Aug 06, 2011 6:53 am

BIT0112-Rokon wrote:A class without encapsulation is more likely a pornography.
Dude, why are you quoting me without my permission? These aren't F/OSQ. Guns
BIT0112-Rokon wrote:I mean, write a library that can be plugged in some other place. or write a utility software.
It's already done in .NET. There's a control named FileSystemWatcher. Drag it onto the form and do whatever you want Head Bang
BIT0112-Rokon wrote:Shakkhor used this in his software for another reason.
I used this because of the nature of my work was highly custom. Bat Doing stuff like creating a report is easier using the provided control. So you probably shouldn't focus on creating a library here. It'll probably be a waste, because someone else already did it.

Instead, why don't you write me a Solomon-Reed implementation? cheers
BIT0130-Shakkhor
BIT0130-Shakkhor
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 209
Points : 328

Back to top Go down

Tutorial: How to watch a directory for events using C#  Empty Re: Tutorial: How to watch a directory for events using C#

Post by BIT0122-Amit Sat Aug 06, 2011 7:38 am

You misquoted me!! Neutral I wrote that last one, not him!!!
And that's what the objective of the tutorial was. :/ I explained it, didn't I?
What I mean is, after reading this, people know how to watch a folder for changes. How is he going to do this, is up to him.

Making customized things :/

I will pass that Solomon Reed to Rokon Very Happy



*feels ignored*
I explained why I didn't use private keyword because it is by default private anyway. No one noticed it Ranting
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Tutorial: How to watch a directory for events using C#  Empty Re: Tutorial: How to watch a directory for events using C#

Post by BIT0112-Rokon Sat Aug 06, 2011 8:51 am

Amit, actually noticed.

Btw right now its not possible to write a Solomon-Reed implementation. I'm busy with an android app so that I can survive next month on God's green earth. I think, Shakkhor has some free time now.

So Shakkhor, help yourself.

and about the quote, nice, you first used the word, but somehow I promote it. So credit goes to me!
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Tutorial: How to watch a directory for events using C#  Empty Re: Tutorial: How to watch a directory for events using C#

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum