setting directory for reader

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

setting directory for reader

 
0
  #1
Jul 21st, 2009
hey, i have my streamreader code, but im running it in a filesystem watcher program,

once a certain file is seen, then read other files in the folder

so i basically have the filesystem watcher set up to wait for this certain file. then i know the reader tries to find a file but it cant. I think the problem must be the reader needing the folder directory that the filesystemwatcher is looking in, but i have no idea how.

for example, here is the code i have, how do i modify it to read files in the folder specified in "DestDir"

  1. namespace ConsoleApplication1
  2. {
  3. class process
  4. {
  5. private static string myString = string.Empty;
  6.  
  7.  
  8. static void Main(string[] args)
  9. {
  10.  
  11.  
  12. Run();
  13. }
  14.  
  15. [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
  16. public static void Run()
  17. {
  18. string[] args = System.Environment.GetCommandLineArgs();
  19.  
  20. // Create a new FileSystemWatcher and set its properties.
  21. FileSystemWatcher watcher = new FileSystemWatcher();
  22.  
  23. string test1 = System.Configuration.ConfigurationSettings.AppSettings["DestDir"];
  24.  
  25.  
  26. watcher.Path = test1;//args[1];
  27. /* Watch for changes in LastAccess and LastWrite times, and
  28.   the renaming of files or directories. */
  29. watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
  30. | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  31. // Only watch text files.
  32. watcher.Filter = "TransferComplete.*";
  33.  
  34. // Add event handlers.
  35. //watcher.Changed += new FileSystemEventHandler(OnChanged);
  36. watcher.Created += new FileSystemEventHandler(OnChanged);
  37. //watcher.Deleted += new FileSystemEventHandler(OnChanged);
  38. //watcher.Renamed += new RenamedEventHandler(OnRenamed);
  39.  
  40. // Begin watching.
  41. watcher.EnableRaisingEvents = true;
  42.  
  43. // Wait for the user to quit the program.lol
  44. Console.WriteLine("Press \'q\' to quit the sample.");
  45. while (Console.Read() != 'q') ;
  46. }
  47.  
  48. // Define the event handlers.
  49. private static void OnChanged(object source, FileSystemEventArgs e)
  50. {
  51.  
  52.  
  53. try
  54. {
  55. // Create an instance of StreamReader to read from a file.
  56. // The using statement also closes the StreamReader.
  57. using (StreamReader sr = new StreamReader("TestFile.txt"))
  58. {
  59. String line;
  60. // Read and display lines from the file until the end of
  61. // the file is reached.
  62. while ((line = sr.ReadLine()) != null)
  63. {
  64. Console.WriteLine(line);
  65. }
  66. }
  67. }
  68. catch (Exception d)
  69. {
  70. // Let the user know what went wrong.
  71. Console.WriteLine("The file could not be read:");
  72. Console.WriteLine(d.Message);
  73. }
  74.  
  75. }
  76.  
  77. }
  78. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,219
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 574
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: setting directory for reader

 
0
  #2
Jul 21st, 2009
e.FullPath gives you the full path of the modified file in private static void OnChanged(object source, FileSystemEventArgs e) . Also since you are using a FileSystemWatcher you know the file is probably in use by another application. In that case you should open the file like this:

  1. //This gives us access to files that are in use by IIS -- sk
  2. using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  3. {
  4. using (StreamReader sr = new StreamReader(fs))
  5. {
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

Re: setting directory for reader

 
0
  #3
Jul 21st, 2009
the filesystem watcher only checks for files being created in that folder, so its not being used by another application.

cheers abotu the e.fullpath thing, how would i actually get/set up the reader to check that directory?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,219
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 574
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: setting directory for reader

 
0
  #4
Jul 21st, 2009
I changed your stream to open e.FullPath . I think you may also want to test that e.FullPath is a file and not a directory.

  1. public static void Run()
  2. {
  3. FileSystemWatcher watcher = new FileSystemWatcher();
  4.  
  5. watcher.Path = @"C:\text\";//args[1];
  6. watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  7. watcher.Filter = "TransferComplete.*";
  8. watcher.Created += new FileSystemEventHandler(OnChanged);
  9. watcher.EnableRaisingEvents = true;
  10.  
  11. Console.WriteLine("q to exit");
  12. while (Console.Read() != 'q')
  13. {
  14. }
  15. watcher.EnableRaisingEvents = false;
  16. watcher.Dispose();
  17. }
  18.  
  19. private static void OnChanged(object source, FileSystemEventArgs e)
  20. {
  21. System.Diagnostics.Debugger.Break();
  22. try
  23. {
  24. using (StreamReader sr = new StreamReader(e.FullPath))
  25. {
  26. String line;
  27. while ((line = sr.ReadLine()) != null)
  28. {
  29. Console.WriteLine(line);
  30. }
  31. }
  32. }
  33. catch (Exception d)
  34. {
  35. Console.WriteLine("The file could not be read:");
  36. Console.WriteLine(d.Message);
  37. }
  38. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

Re: setting directory for reader

 
0
  #5
Jul 21st, 2009
To get the folder not the file i needed:

  1. Path.GetDirectoryName(e.FullPath)

as you predicted, access to the file was denied, although i dont know why, its not actually in use by another application? and i cant use the code you gave me as i cant specify a filename, as it is eventually going read the lines of each file in the folder ive got it to point to.

how could i get passed that?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,219
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 574
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: setting directory for reader

 
0
  #6
Jul 21st, 2009
Why can't you specify the filename? You are obviously opening a file .. so whether you iterate every file in the directory, open a hard coded file, or anything just change the streams you use to the original streams I posted.

  1. using (StreamReader sr = new StreamReader("TestFile.txt"))

Change that stream line to the two streams I posted earlier if you're using a hard coded file?

I think you need to explain what you are trying to do because this make absolutely no sense so i'm unable to give you better answers.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

Re: setting directory for reader

 
0
  #7
Jul 21st, 2009
i am basically monitoring a folder, and there are files being passed into it, but when a file comes in called "TransferComplete" i want it to go through the other files in the folder, taking whats in them and putting them all into 1 different file in another folder,

right now im working on just reading to the console what is in each file. but i am having trouble,

does that clear things up?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,219
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 574
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: setting directory for reader

 
0
  #8
Jul 21st, 2009
  1. private static void OnChanged(object source, FileSystemEventArgs e)
  2. {
  3. DirectoryInfo dr = new DirectoryInfo(Path.GetDirectoryName(e.FullPath));
  4. FileInfo[] files = dr.GetFiles("TransferComplete.*");
  5. foreach (FileInfo file in files)
  6. {
  7. try
  8. {
  9. //This gives us access to files that are in use by IIS -- sk
  10. using (FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  11. {
  12. using (StreamReader sr = new StreamReader(fs))
  13. {
  14. string line;
  15. while ((line = sr.ReadLine()) != null)
  16. {
  17. Console.WriteLine(line);
  18. }
  19. }
  20. }
  21. }
  22. catch (Exception d)
  23. {
  24. Console.WriteLine("The file could not be read:");
  25. Console.WriteLine(d.Message);
  26. continue;
  27. }
  28. }
  29. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC