| | |
Streamwriter isnt working correctly
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 63
Reputation:
Solved Threads: 0
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:
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.
here is the code i have so far:
C# Syntax (Toggle Plain Text)
[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
Call the overloaded method of
The above code snippet appends to a file instead of overwriting it.
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: c# Syntax (Toggle Plain Text)
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.
I don't see why not. You should make this change though:
This way you keep the streamwriter open until you complete writing the file.
c# Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- ASP not working correctly. (Visual Basic 4 / 5 / 6)
- My IF ($email_settings == "0") isnt working! :( (PHP)
- why isnt it working!?! (C++)
- my html code isnt working please help (JavaScript / DHTML / AJAX)
- msn messenger 7.0 isnt working (Windows 95 / 98 / Me)
- CD-ROm isnt working correctly (Storage)
Other Threads in the C# Forum
- Previous Thread: Send value...
- Next Thread: How to Unzip a File ??
| Thread Tools | Search this Thread |
.net access algorithm animation array barchart bitmap box broadcast buttons c# check checkbox client code combobox control conversion csharp custom database datagrid datagridview dataset datastructure datetime degrees development directrobot draganddrop drawing encryption enum event excel file form format forms function gdi+ hash httpwebrequest image index input install java label lisp list listbox mandelbrot math mouseclick mp3 mysql native operator path photoshop picturebox pixelinversion post print process programming radians regex remote remoting richtextbox safari server sleep snooze socket sql statistics stream string table tables tcp text textbox thread time timer update usercontrol usercontrols validation visualstudio webbrowser wfa windows winforms wpf xml






