943,923 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 366
  • C# RSS
Jul 21st, 2009
0

setting directory for reader

Expand Post »
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"

C# Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Jul 21st, 2009
0

Re: setting directory for reader

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:

c# Syntax (Toggle Plain Text)
  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. {
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 21st, 2009
0

Re: setting directory for reader

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

Re: setting directory for reader

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.

c# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 21st, 2009
0

Re: setting directory for reader

To get the folder not the file i needed:

C# Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Jul 21st, 2009
0

Re: setting directory for reader

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.

c# Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 21st, 2009
0

Re: setting directory for reader

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

Re: setting directory for reader

c# Syntax (Toggle Plain Text)
  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. }
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: Machine reading Implementation in C#
Next Thread in C# Forum Timeline: How to Create a menu addIn for word in .Net





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


Follow us on Twitter


© 2011 DaniWeb® LLC