0

I have some code which outputs data from the FileSystemWatcher class, although each times to outputs a line to test.txt, it overwrites the previous entry, is there a way to stop this happening?

Code in question:

static void FileWatcher_Created(object sender, FileSystemEventArgs e)
{

    using (StreamWriter fileWriter = new StreamWriter("test3.txt"))
    {
        var data = true;
        fileWriter.Write("C:\\" + e.Name + Environment.NewLine);
    }

Full Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileWatcherr
{
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


        Console.ReadKey(); 
    }



        static void FileWatcher_Renamed(object sender, RenamedEventArgs e)
        {
            Console.WriteLine(e.OldName + " was renamed to " + e.Name);
        }

        static void FileWatcher_Deleted(object sender, FileSystemEventArgs e)

        {
            Console.WriteLine(e.Name + " was deleted");
        }

        static void FileWatcher_Created(object sender, FileSystemEventArgs e)
        {

            using (StreamWriter fileWriter = new StreamWriter("test3.txt"))
            {
                var data = true;
                fileWriter.Write("C:\\" + e.Name + Environment.NewLine);
            }

        }

        static void FileWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine(e.Name + "");
        }
    }
}
1
  • As lots of people have suggested the same thing, you can assume that it is correct. Something else is a problem for you, so please add info of your new problem Commented May 20, 2011 at 19:44

5 Answers 5

3

Edit your code to be the below. The second parameter to the StreamWriter is if we should append or overwrite. Set to true to append and false to overwrite (default, thus your problem)

   using (StreamWriter fileWriter = new StreamWriter("test3.txt", true))
    {
        var data = true;
        fileWriter.WriteLine(Path.Combine("C:\\", e.Name));
    }

More info about this constructor can be found here http://msdn.microsoft.com/en-us/library/36b035cb.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

@Michael what is the problem? I found a syntax error I fixed, try the current code
0

Try

using (StreamWriter fileWriter = new StreamWriter("test3.txt", true))
{
...
}

Comments

0

Replace

new StreamWriter("test3.txt")

by

new StreamWriter("test3.txt", true)

This will make the stream append to the file.

Comments

0

Consider opening file to append:

    using (StreamWriter fileWriter = new StreamWriter("test3.txt", true))
    {
        var data = true;
        fileWriter.WriteLine("C:\\" + e.Name);
    }

Note that it is better to use WriteLine method.

2 Comments

Use string.format or with Concat function instead of concatenating strings your way
@dario Even though it is correct that you should not just add together strings, combining paths is usually recommended to be done with Path.Combine
0

The StreamWriter class has an overload constructor that takes a second bool parameter which specifies if you want to append to an existing file. More information here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.