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);
}
}
Checkout these articles:
FileSystemWatcher
seems to be an answer
LINK
Straight from documentation
using System;
using System.IO;
using System.Security.Permissions;
public class Watcher
{
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
// If a directory is not specified, exit program.
if(args.Length != 2)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: Watcher.exe (directory)");
return;
}
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
These might help with WMI based approach.
http://www.codeproject.com/Articles/42212/WMI-and-File-System-Monitoring
http://msdn.microsoft.com/en-us/library/aa394594(v=vs.85).aspx
http://www.csharphelp.com/2006/10/wmi-made-easy-for-c/
Recursive implementation for all files in all folders on a drive:
using System;
using System.IO;
using System.Security.Permissions;
using System.Threading.Tasks;
public class Watcher
{
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
// Create a new FileSystemWatcher and set its properties.
RecurseDirectory();
Console.ReadKey();
}
private static void RecurseDirectory(string path = @"T:\")
{
try
{
SetWatcher(path);
Parallel.ForEach(Directory.GetDirectories(path), RecurseDirectory);
}
catch (Exception e)
{
//Ignore
}
}
private static void SetWatcher(string path)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
//watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With