|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
The DirectoryMirror ApplicationThis application creates and maintains a mirror of a selected directory; it monitors IO activity in the specified directory and reacts to this in order to keep a copy of the contents (including subfolders) in another directory. This is an implementation of the I've created a class called There are eight different It's a well known bug that file paths returned by the event arguments Believe it or not, I actually had a practical use for this little application when I created it! I might also say that I created the need because I had been itching to experiment with the The directoryMirror classThe using System;
using System.IO;
namespace mirror
{
// This class is based on the FileSystemWatcher class
public class directoryMirror: System.IO.FileSystemWatcher
{
// Path of the mirror directory
string mirDir = "";
// Source directory
public string sourceDirectory
{
get { return this.Path; }
set { this.Path = value; }
}
// Mirror Directory
public string mirrorDirectory
{
get { return mirDir; }
set { mirDir = value; }
}
// Delegate and event to send messages about IO activity
// and various exceptions.
public delegate void infoMessageDelegate(string infoMessage);
public event infoMessageDelegate infoMessageEvent;
public directoryMirror(string srcDir, string mirDir)
{
// Set up the path to the source directory
sourceDirectory = srcDir;
// Set up the path to the mirror directory
mirrorDirectory = mirDir;
// Set up the different properties
// Monitor all files and directories
this.Filter = "";
// Listen for changes in the name of files and directories
// and changes in date/time of the last modification.
this.NotifyFilter =
((System.IO.NotifyFilters)((System.IO.NotifyFilters.FileName |
System.IO.NotifyFilters.DirectoryName |
System.IO.NotifyFilters.LastWrite)));
this.IncludeSubdirectories = true;
this.EnableRaisingEvents = true;
// Set up the handlers for the FileSystemWatcher events
this.Changed += new FileSystemEventHandler(fsw_onChanged);
this.Created += new FileSystemEventHandler(fsw_onCreated);
this.Deleted += new FileSystemEventHandler(fsw_onDeleted);
this.Renamed += new RenamedEventHandler(fsw_onRenamed);
}
private void fsw_onChanged(object sender, System.IO.FileSystemEventArgs e)
{
// Because of the NotifyFilters we are using, this event can only
// pertain to a file, not a directory
try
{
if(!System.IO.Directory.Exists(e.FullPath))
// It's a file
{
// Destination to copy the file to
string destination =
e.FullPath.Replace(sourceDirectory, mirrorDirectory);
// Overwrite or copy the file in the mirror directory
System.IO.File.Copy(e.FullPath, destination, true);
// Send a message to report this activity
infoMessageEvent("\r\n" + e.ChangeType + " " + e.FullPath);
}
}
catch(DirectoryNotFoundException iox)
{
infoMessageEvent("\r\nEXCEPTION " +
"(onChanged): Directory No Found , " + iox.Message);
}
catch(FileNotFoundException iox)
{
infoMessageEvent("\r\nEXCEPTION" +
" (onChanged): File Not Found, " + iox.Message);
}
catch(IOException iox)
{
infoMessageEvent("\r\nEXCEPTION " +
"(onChanged): IO Error, " + iox.Message);
}
catch(Exception ex)
{
infoMessageEvent("\r\nEXCEPTION " +
"(onChanged): " + ex.Message);
}
}
private void fsw_onCreated(object sender,
System.IO.FileSystemEventArgs e)
{
try
{
if(System.IO.Directory.Exists(e.FullPath))
// It's a directory
{
// Create a matching directory in the mirror directory
System.IO.Directory.CreateDirectory(
e.FullPath.Replace(sourceDirectory, mirrorDirectory));
// Send a message to report this activity
infoMessageEvent("\r\n" +
e.ChangeType + ", " + e.FullPath);
}
else
// It's a file
{
// Destination to copy the file to
string destination =
e.FullPath.Replace(sourceDirectory, mirrorDirectory);
// Copy the file to the mirror directory
System.IO.File.Copy(e.FullPath, destination, true);
// Send a message to report this activity
infoMessageEvent("\r\n" + e.ChangeType + ", " + e.FullPath);
}
}
catch(DirectoryNotFoundException iox)
{
infoMessageEvent("\r\nEXCEPTION" +
" (onCreated): DIRECTORY NOT FOUND , " + iox.Message);
}
catch(FileNotFoundException iox)
{
infoMessageEvent("\r\nEXCEPTION (onCreated): " +
"FILE NOT FOUND, " + iox.FileName + ", " + iox.Message);
}
catch(IOException iox)
{
infoMessageEvent("\r\nEXCEPTION " +
"(onCreated): IO ERROR, " + iox.Message);
}
catch(Exception ex)
{
infoMessageEvent("\r\nEXCEPTION" +
" (onCreated): " + ex.Message);
}
}
private void fsw_onDeleted(object sender,
System.IO.FileSystemEventArgs e)
{
try
{
if(System.IO.Directory.Exists(
e.FullPath.Replace(sourceDirectory,
mirrorDirectory)))
// It's a directory
{
System.IO.Directory.Delete(
e.FullPath.Replace(sourceDirectory,
mirrorDirectory), true);
// Send a message to report this activity
infoMessageEvent("\r\n" + e.ChangeType + ", " + e.FullPath);
}
else
// It's a file
{
string destination =
e.FullPath.Replace(sourceDirectory,
mirrorDirectory);
System.IO.File.Delete(destination);
// Send a message to report this activity
infoMessageEvent("\r\n" + e.ChangeType + ", " + e.FullPath);
}
}
catch(DirectoryNotFoundException iox)
{
infoMessageEvent("\r\nEXCEPTION" +
" (onDeleted): DIRECTORY NOT FOUND, "
+ iox.Message);
}
catch(FileNotFoundException iox)
{
infoMessageEvent("\r\nEXCEPTION (onDeleted):" +
" FILE NOT FOUND, " + iox.FileName +
", " + iox.Message);
}
catch(IOException iox)
{
infoMessageEvent("\r\nEXCEPTION " +
"(onDeleted): IO ERROR, " + iox.Message);
}
catch(Exception ex)
{
infoMessageEvent("\r\nEXCEPTION" +
" (onDeleted): " + ex.Message);
}
}
private void fsw_onRenamed(object sender, RenamedEventArgs e)
{
try
{
if(System.IO.Directory.Exists(e.FullPath))
// It's a directory
{
if(System.IO.Directory.Exists(
e.OldFullPath.Replace(sourceDirectory,
mirrorDirectory)))
{
string oldFPath =
e.OldFullPath.Replace(sourceDirectory,
mirrorDirectory);
string newFPath = e.FullPath.Replace(sourceDirectory,
mirrorDirectory);
System.IO.Directory.Move(oldFPath, newFPath);
// Send a message to report this activity
infoMessageEvent("\r\n" + e.ChangeType + ", " + e.FullPath);
}
}
else
// It's a file
{
string oldFPath =
e.OldFullPath.Replace(sourceDirectory, mirrorDirectory);
string newFPath =
e.FullPath.Replace(sourceDirectory, mirrorDirectory);
System.IO.File.Move(oldFPath, newFPath);
// Send a message to report this activity
infoMessageEvent("\r\n" + e.ChangeType + ", " + e.FullPath);
}
}
catch(DirectoryNotFoundException iox)
{
infoMessageEvent("\r\nEXCEPTION" +
" (onRenamed): DIRECTORY NOT FOUND, " + iox.Message);
}
catch(FileNotFoundException iox)
{
infoMessageEvent("\r\nEXCEPTION" +
" (onRenamed): FILE NOT FOUND, " + iox.FileName +
", " + iox.Message);
}
catch(IOException iox)
{
infoMessageEvent("\r\nEXCEPTION" +
" (onRenamed): IO ERROR, " + iox.Message);
}
catch(Exception ex)
{
infoMessageEvent("\r\nEXCEPTION (onRenamed): "
+ ex.Message);
}
}
}
}
Points of InterestBy watching the messages sent back by the
|
||||||||||||||||||||||