Reputation: 57
I use FileSystemWatcher
for watch on File System. it can watch on particular folder or drive.
But I want it on whole file system means it should watch on all drive.
Any idea about this ?
I do that much.
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
if (args.Length < 2)
{
Console.WriteLine("Usage: Watcher.exe PATH [...] [PATH]");
return;
}
List<string> list = new List<string>();
for (int i = 1; i < args.Length; i++)
{
list.Add(args[i]);
}
foreach (string my_path in list)
{
WatchFile(my_path);
}
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
private static void WatchFile(string watch_folder)
{
watcher.Path = watch_folder;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.xml";
watcher.Changed += new FileSystemEventHandler(convert);
watcher.EnableRaisingEvents = true;
}
using Filesystem Watcher - Multiple folders
Upvotes: 2
Views: 1824
Reputation: 134
You can watch entire system with using IncludeSubdirectories to Logical Drives. Try this code,
string[] drives = Environment.GetLogicalDrives();
foreach(string drive in drives)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = drive;
watcher.IncludeSubdirectories = true;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.txt";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents = true;
}
Upvotes: 2
Reputation: 1409
One way could be to Enumerate over all directories and watch all of them by using FileSystemWatcher
on each of them.
But it will consume lots of resources. So you can, for an alternate look into this link:Filewatcher for the whole computer (alternative?)
Upvotes: 3