0

I need to create a listener in C# that will watch a shared folder (UNC path) and copy the files with a specific extension (*.json) to a target folder when they arrive. The files can be delayed for about half a minute. The folder is never empty.

Problems:

  1. The files will arrive in a new sub folder, FileSystemWatcher can't be used since it can't listen to sub folders in a shared folder.

  2. The files needs to be copied and left in the folder, so we need to assure that the same file isn't copied more than once.

  3. Files that are edited/updated needs to be copied again and overwritten in the target folder.

  4. Other files will be in the folder and new files will arrive that we need to ignore (doesn't have the right extension).

I thought about polling the folder , but I didn't come up with a good implementation.

I'm pretty sure I can't use the FilesystemWatcher object, but maybe someone can find a smart solution for using it.

Shahar
  • 655
  • 2
  • 7
  • 23
  • 1
    have you seen [this](http://www.c-sharpcorner.com/article/monitoring-file-system-using-filesystemwatcher-class-part2/) or [this](https://stackoverflow.com/questions/35835090/filesystemwatcher-includesubdirectories-not-working-on-network-share). – igorc Jul 14 '17 at 06:54
  • I have seen them, and don't like these solutions. I'm looking for a solution without FileSystemWatcher... – Shahar Jul 16 '17 at 07:07
  • 1
    Without a `FileSystemWatcher`, polling for a specific file, or for new files, is going to be your only option. You will need to find a rate that is responsive enough for your application, but not heavy enough to bog down your file server and network. – Bradley Uffner Jul 20 '17 at 18:09

1 Answers1

2

One solution to your problem could be you can check the location constantly for a while and examine changes by yourself.

it is not a complete solution, but an idea to consider.

    public async Task FindMyFile(string filePath)
    {           
        int retries = 0;
        this.Founded = false;
        while (!this.Founded)
        {
            if (System.IO.File.Exists(filePath))
                this.Founded = true;
            else if (retries < this.maxTries)
            {
                Console.WriteLine($"File {filePath} not found. Going to wait for 15 minutes");
                await Task.Delay(new TimeSpan(0, 15, 0));
                ++retries;
            }
            else
            {
                Console.WriteLine($"File {filePath} not found, retries exceeded.");
                break;
            }
        }
    }
Dinch
  • 548
  • 4
  • 9