Some events like creation, deletion or renaming of files and folders can be tracked using FileSystemWatcher
, but I also need to keep track of copy and paste for the same.
I am not looking to track what content is copied from a file like .text .doc etc., I want to keep track of entire files or folders, like d:\folder\movies
is copied to e:\movies
.
below is the code sample which will be able to keep log of any file operation like rename ,delete,created,changed.Iam looking for copy/paste and move alos.
class Program
{
static void Main(string[] args)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
ArrayList list = new ArrayList();
String s;
foreach (DriveInfo d in allDrives)
{
if (DriveType.Fixed == d.DriveType)
{
s = Convert.ToString(d);
list.Add(s);
Console.WriteLine(s.TrimEnd());
}
}
run(list);
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void run(ArrayList list)
{
ArrayList fw = new ArrayList();
FileSystemWatcher Clientwatcher = new FileSystemWatcher();
foreach (string s in list)
{
String temp=s;
temp=temp+'\\';
Clientwatcher.Path =temp;
Clientwatcher.Filter = "*.*";
Clientwatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
Clientwatcher.Changed += new FileSystemEventHandler(Clientwatcher_Changed);
Clientwatcher.Renamed += new RenamedEventHandler(Clientwatcher_Renamed);
Clientwatcher.Created += new FileSystemEventHandler(Clientwatcher_Created);
Clientwatcher.Deleted += new FileSystemEventHandler(Clientwatcher_Deleted);
Clientwatcher.EnableRaisingEvents = true;
fw.Add(Clientwatcher);
}
Console.WriteLine(fw.Capacity);
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
static void Clientwatcher_Deleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
string s = e.FullPath + e.ChangeType;
File.AppendAllText("c:\\temp.txt", s);
}
static void Clientwatcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
string s = e.FullPath + e.ChangeType;
File.AppendAllText("c:\\temp.txt", s);
}
static void Clientwatcher_Renamed(object sender, RenamedEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType + e.OldFullPath);
string s = e.OldFullPath + " " + e.FullPath + " " + DateTime.Now + Environment.NewLine;
File.AppendAllText("c:\\temp.txt", s);
}
static void Clientwatcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
string s = e.FullPath + " " + e.ChangeType + " " + DateTime.Now + Environment.NewLine;
File.AppendAllText("c:\\temp.txt", s);
}
}