FileSystemWatcher - How to pipe output to text file?

Someone asked on Stack Overflow:

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();
    }
}

I posted the following answer, which was chosen as the accepted answer and received 2 upvotes:

You need to call

fileWriter.Write(data);

Additionally, you should wrap it up like this:

using(StreamWriter fileWriter = new StreamWriter("test.txt"))
{
    fileWriter.Write(data);
    fileWriter.Flush(); // maybe not necessary
}

This will write data to the filesystem and it should trigger your FileSystemWatcher object.

edit — inplace example

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;      

        // updated code
        using(StreamWriter fileWriter = new StreamWriter("test.txt"))
        {
            var data = true;
            fileWriter.Write(data);
        }

        Console.ReadKey(); 
    }
}
Notable comments

Nate (0 upvotes): Where are you defining e? I’m assuming the code sample you provided was dumbed down. If that is truely your entire code, there is no e object defined at the same scope that fileWriter is defined and that error is to be expected.

Nate (0 upvotes): Try something like fileWriter.Write(e.OldName + " was renamed to " + e.Name + Environment.NewLine);

Nate (0 upvotes): @Michael — yes, you can change that to whatever you want, its just an example. The StreamWriter.Write(); method has many overloads that take different parameters.

Nate (0 upvotes): @Michael glad to hear it. Good luck.


Originally posted on Stack Overflow — 2 upvotes (accepted answer). Licensed under CC BY-SA.

signed letter b

Dad. Geek. Gamer. Software developer. Cloud user. Old Car enthusiast.  Blogger.


Top Posts


profile for Nate on Stack Exchange, a network of free, community-driven Q&A sites
a proud member of the blue team of 512KB club
Thoughts, opinions, and ideas shared here are my own. © 2026 Nate Bross.