Tutorial: How to watch a directory for events using C#
3 posters
Page 1 of 1
Tutorial: How to watch a directory for events using C#
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:
Feed backs are welcome
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
Re: Tutorial: How to watch a directory for events using C#
This is a nice fragment man. Rep++
BIT0130-Shakkhor- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 209
Points : 328
Re: Tutorial: How to watch a directory for events using C#
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
This topic has been here for a bit more than a month. Good enough time to take a look I think.
Or paying respect to rules decided by everyone instead of forcing this tutorial to the tutorials zone was a mistake?
But null reply is a bit mystifying
This topic has been here for a bit more than a month. Good enough time to take a look I think.
Or paying respect to rules decided by everyone instead of forcing this tutorial to the tutorials zone was a mistake?
Re: Tutorial: How to watch a directory for events using C#
I've used this fragment in my Printer Driver.
BIT0130-Shakkhor- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 209
Points : 328
Re: Tutorial: How to watch a directory for events using C#
Glad to know.
And yet, this is not accepted as a tutorial by the honorable moderators
And yet, this is not accepted as a tutorial by the honorable moderators
Re: Tutorial: How to watch a directory for events using C#
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++
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++
Re: Tutorial: How to watch a directory for events using C#
First of all, I am honored 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 So, erm.. you meant make a library which explains using a library?
About accessibility,
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 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.
Re: Tutorial: How to watch a directory for events using C#
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.
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.
Re: Tutorial: How to watch a directory for events using C#
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.
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.
Re: Tutorial: How to watch a directory for events using C#
Dude, why are you quoting me without my permission? These aren't F/OSQ.BIT0112-Rokon wrote:A class without encapsulation is more likely a pornography.
It's already done in .NET. There's a control named FileSystemWatcher. Drag it onto the form and do whatever you wantBIT0112-Rokon wrote:I mean, write a library that can be plugged in some other place. or write a utility software.
I used this because of the nature of my work was highly custom. 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.BIT0112-Rokon wrote:Shakkhor used this in his software for another reason.
Instead, why don't you write me a Solomon-Reed implementation?
BIT0130-Shakkhor- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 209
Points : 328
Re: Tutorial: How to watch a directory for events using C#
You misquoted me!! I wrote that last one, not him!!!
And that's what the objective of the tutorial was. :/ I explained it, didn't I?
Making customized things :/
I will pass that Solomon Reed to Rokon
*feels ignored*
I explained why I didn't use private keyword because it is by default private anyway. No one noticed it
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
*feels ignored*
I explained why I didn't use private keyword because it is by default private anyway. No one noticed it
Re: Tutorial: How to watch a directory for events using C#
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!
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!
Similar topics
» Watch Cricket Score directly from Ubuntu Desktop Panel
» Introducing Calendar Events in Forum
» MPI tutorial
» Assembly Tutorial
» IP address tutorial
» Introducing Calendar Events in Forum
» MPI tutorial
» Assembly Tutorial
» IP address tutorial
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford