Visual C# : File System Watcher (basics)

Assumed Knowledge: Very little knowledge of C#.
Information: Listens to the file system notifications and raises events when a directory, or file in a directory, has changed, been created, renamed and even deleted.
requirements: fileSystemWatcher module.

Posted Image


Step 1: Adding functions
Form 1: - Load

                        fileSystemWatcher1
.Path = "C:/";

                        fileSystemWatcher1
.Filter = "*.*";


                        fileSystemWatcher1
.IncludeSubdirectories = true;

                       
// Allows the file system watcher to search though sub firectories


                        fileSystemWatcher1
.EnableRaisingEvents = true;

                       
// Gets or sets a value indicating whether the component is enabled.
This sets the path and the filter to pick up certain file formats, by setting the subdirectories true the fileSystemWatcher will search though all sub directories, Enable raising event gets or sets a value indicating whether the component is enabled.

Step 2: adding unimplemented methods
private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)

               
{

                        listBox1
.Items.Add("File (" + e.ChangeType + ") : " + e.FullPath);

                       
// Tells the user that something in the system has been changed.

               
}


               
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)

               
{

                        listBox1
.Items.Add("File (" + e.ChangeType + ") : " + e.FullPath);

                       
// Tells the user that something in the system has been Created.

               
}


               
private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)

               
{

                        listBox1
.Items.Add("File (" + e.ChangeType + ") : " + e.FullPath);

                       
// Tells the user that something in the system has been Deleted.

               
}


               
private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)

               
{

                        listBox1
.Items.Add("File (" + e.ChangeType + ") : " + e.FullPath);

                       
// Tells the user that something in the system has been renamed.

               
}
[Note: These methods may need to be written - not c&p]
These methods, will tell the user if a file is changed, created, renamed and even deleted. 

No comments:

Post a Comment