i have a filesystemwatcher looking for a file, then once the file comes into the folder ive set, it then reads all the lines of data from inside each file in the folder being monitored. each read line should then be writen into another file in a different folder. so i have one file full of all the data from the files in the monitored folder!

here is the code i have so far:

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public static void Run()
        {
            string[] args = System.Environment.GetCommandLineArgs();

            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();
            
            string test1 = System.Configuration.ConfigurationSettings.AppSettings["DestDir"];
            
            
            watcher.Path = test1;//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 = "1.*";

            // 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.lol
            Console.WriteLine("Press \'q\' to quit the sample.");
            while (Console.Read() != 'q') ;
        }

        // Define the event handlers.
        private static void OnChanged(object source, FileSystemEventArgs e)
        {

            File.Delete(e.FullPath);
            //string[] files = System.IO.Directory.GetFiles(Path.GetDirectoryName(e.FullPath));  

           
                DirectoryInfo dr = new DirectoryInfo(Path.GetDirectoryName(e.FullPath)); 
                FileInfo[] files1 = dr.GetFiles("*.*");
                foreach (FileInfo file in files1)
                {

                    try
                    {
                        using (FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {

                            // Create an instance of StreamReader to read from a file.
                            // The using statement also closes the StreamReader.
                            using (StreamReader sr = new StreamReader(fs))
                            {
                                String line;
                                // Read and display lines from the file until the end of 
                                // the file is reached.
                                while ((line = sr.ReadLine()) != null)
                                {
                                    Console.WriteLine(line);
                                    // add streamwrite to put each line into another file
                                    
                                    
                                    using (StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\chris.kennedy\Desktop\process\Work.txt"))
                                    {
                                        //foreach (FileInfo file in files1)
                                       // {
                                        
                                        sw.WriteLine("\n");
                                        sw.WriteLine(line + "\n");
                                        Console.WriteLine("im doing it ok");

                                       // }
                                    }

the sw.writeline appears only to write the last line of the last file, hence the \n to make it return , so that no overwriting goes on. but that doesnt work either, i also checked that the files in the monotored folder had no spaces or returns after the data to make sure that didnt affect it.

You are overwriting your file. You should refactor your code to keep the StreamWriter open instead of open/closing it for every line you want to write.
Call the overloaded method of StreamWriter with the second parameter as "true" for append such as this:

using (StreamWriter sw = new StreamWriter(@"C:\test.txt", true))
      {
        sw.WriteLine("abc 123 123");
      }

The above code snippet appends to a file instead of overwriting it.

aw thank you so much. totally worked! cheers!

is this robust enough to handle me putting csv files into a big excel file, obviously if i chance the .txt to .csv and set the folders up as they should be etc?

I don't see why not. You should make this change though:

using (StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\chris.kennedy\Desktop\process\Work.txt", true))
      {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
          Console.WriteLine(line);
          sw.WriteLine("\n");
          sw.WriteLine(line + "\n");
          Console.WriteLine("im doing it ok");
        }
      }

This way you keep the streamwriter open until you complete writing the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.