I am using the FileSystemWatcher Class. I am trying to pipe the output to a text file. I have added the StreamWriter fileWriter = new StreamWriter("test.txt"); but nothing is output to the file! Where am I going wrong?
class Program
{
static void Main(string[] args)
{
string dirPath = "C:\\";
FileSystemWatcher fileWatcher = new FileSystemWatcher(dirPath);
fileWatcher.IncludeSubdirectories = true;
fileWatcher.Filter = "*.exe";
// fileWatcher.Filter = "C:\\$Recycle.Bin";
// fileWatcher.Changed += new FileSystemEventHandler(FileWatcher_Changed);
fileWatcher.Created += new FileSystemEventHandler(FileWatcher_Created);
// fileWatcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted);
// fileWatcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);
fileWatcher.EnableRaisingEvents = true;
StreamWriter fileWriter = new StreamWriter("test.txt");
Console.ReadKey();
}
}
You need to call
Additionally, you should wrap it up like this:
This will write data to the filesystem and it should trigger your FileSystemWatcher object.
edit — inplace example