943,522 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1018
  • C# RSS
Jul 22nd, 2009
0

Streamwriter isnt working correctly

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  1.  
  2. [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
  3. public static void Run()
  4. {
  5. string[] args = System.Environment.GetCommandLineArgs();
  6.  
  7. // Create a new FileSystemWatcher and set its properties.
  8. FileSystemWatcher watcher = new FileSystemWatcher();
  9.  
  10. string test1 = System.Configuration.ConfigurationSettings.AppSettings["DestDir"];
  11.  
  12.  
  13. watcher.Path = test1;//args[1];
  14. /* Watch for changes in LastAccess and LastWrite times, and
  15.   the renaming of files or directories. */
  16. watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
  17. | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  18. // Only watch text files.
  19. watcher.Filter = "1.*";
  20.  
  21. // Add event handlers.
  22. //watcher.Changed += new FileSystemEventHandler(OnChanged);
  23. watcher.Created += new FileSystemEventHandler(OnChanged);
  24. //watcher.Deleted += new FileSystemEventHandler(OnChanged);
  25. //watcher.Renamed += new RenamedEventHandler(OnRenamed);
  26.  
  27. // Begin watching.
  28. watcher.EnableRaisingEvents = true;
  29.  
  30. // Wait for the user to quit the program.lol
  31. Console.WriteLine("Press \'q\' to quit the sample.");
  32. while (Console.Read() != 'q') ;
  33. }
  34.  
  35. // Define the event handlers.
  36. private static void OnChanged(object source, FileSystemEventArgs e)
  37. {
  38.  
  39. File.Delete(e.FullPath);
  40. //string[] files = System.IO.Directory.GetFiles(Path.GetDirectoryName(e.FullPath));
  41.  
  42.  
  43. DirectoryInfo dr = new DirectoryInfo(Path.GetDirectoryName(e.FullPath));
  44. FileInfo[] files1 = dr.GetFiles("*.*");
  45. foreach (FileInfo file in files1)
  46. {
  47.  
  48. try
  49. {
  50. using (FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
  51. {
  52.  
  53. // Create an instance of StreamReader to read from a file.
  54. // The using statement also closes the StreamReader.
  55. using (StreamReader sr = new StreamReader(fs))
  56. {
  57. String line;
  58. // Read and display lines from the file until the end of
  59. // the file is reached.
  60. while ((line = sr.ReadLine()) != null)
  61. {
  62. Console.WriteLine(line);
  63. // add streamwrite to put each line into another file
  64.  
  65.  
  66. using (StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\chris.kennedy\Desktop\process\Work.txt"))
  67. {
  68. //foreach (FileInfo file in files1)
  69. // {
  70.  
  71. sw.WriteLine("\n");
  72. sw.WriteLine(line + "\n");
  73. Console.WriteLine("im doing it ok");
  74.  
  75. // }
  76. }

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Jul 22nd, 2009
0

Re: Streamwriter isnt working correctly

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:
c# Syntax (Toggle Plain Text)
  1. using (StreamWriter sw = new StreamWriter(@"C:\test.txt", true))
  2. {
  3. sw.WriteLine("abc 123 123");
  4. }

The above code snippet appends to a file instead of overwriting it.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 22nd, 2009
0

Re: Streamwriter isnt working correctly

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Jul 22nd, 2009
0

Re: Streamwriter isnt working correctly

I don't see why not. You should make this change though:
c# Syntax (Toggle Plain Text)
  1. using (StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\chris.kennedy\Desktop\process\Work.txt", true))
  2. {
  3. string line;
  4. while ((line = sr.ReadLine()) != null)
  5. {
  6. Console.WriteLine(line);
  7. sw.WriteLine("\n");
  8. sw.WriteLine(line + "\n");
  9. Console.WriteLine("im doing it ok");
  10. }
  11. }

This way you keep the streamwriter open until you complete writing the file.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Send value...
Next Thread in C# Forum Timeline: How to Unzip a File ??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC