0

I would like to make the FileSystemWatcher monitor a folder and once there is a file created inside to get the filename so after that file could be read by File.OpenRead() and a StreamReader

I have seen a lot of examples but not my case exactly:

        public static void FileWatcher()
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = @"C:\Users\";
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.Filter = "*.txt";
        watcher.Created += new FileSystemEventHandler(OnCreated);
        watcher.EnableRaisingEvents = true;
    }

    public static void OnCreated(object source, FileSystemEventArgs e)
    {
        string filename = Path.GetFileName(e.FullPath);
    }

My question is how to get the filename that is going to be created in that folder and pass its path and file to since the event method is a void:

const string fileName = @"C:\Users\.txt";
        using (var stream = File.OpenRead(fileName))
        using (var reader = new StreamReader(stream))

Any help appreciated!

2
  • Just call a function and pass in the file path like void processfile(string file2process)... What part are you having issues with? Here are the specs..msdn.microsoft.com/en-us/library/… Commented Mar 30, 2017 at 17:43
  • What I don't understand is how to get the file name and path so I could pass it to a method that deals with the reading as string that would look like - string fileName = @"C:\Users\Desktop\Project\standard.txt"; Commented Mar 30, 2017 at 18:02

1 Answer 1

1

I'd recommend encapsulating your file processing in a separate class.

public class FileProcessor
{
      public void ProcessFile(string filePath)
       {
           using (var stream = File.OpenRead(filePath))
           //etc...

       }
}

Then instance it and call it.

 public static void OnCreated(object source, FileSystemEventArgs e)
    {
       var processor = new FileProcessor();
       processor.ProcessFile(e.FullPath);
    }

Or if your method is in the same class as the Created event

YourMethodName(e.FullPath);

And your method should have a signature like this

void YourMethodName (string filePath)
{
  //Process file
}
Sign up to request clarification or add additional context in comments.

3 Comments

I already have a method for that, but I am not sure how to get the string filePath and pass it to it from that OnCreate event.
Can you post your code perhaps?. My example shows how to pass the file path (e.FullPath) from the created event in to a method as a string. If your method is not in another class then just call it. I've updated my answer
I managed to do it with your guidance, and got an IOException, that the file is in use by another process, which meant that the file is locked because it is still copying/creating? Thread.Sleep(2300); solved it. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.