1

I'm facing an issue with filewatcher. My requirement is that when we copy a large folder of size one GB or more than that, FSW should log only one change that folder X has created, but not give the created events of files underneath X. And I wanted to calculate MD5 Checksums of the all those files which are copied with "X" folder. As copying of large files is taking a lot of time I'm not able to get all the files and sub folder names under X. I need to get all the filepaths to add to a dictionary data structure. Could you please advise on this.

    public static string[] GetFilesAndFolders(string path)
    {
        foreach (string dirs in Directory.GetDirectories(path))
        {
            fileandFolderNames[counter] = dirs;
            counter++;
            foreach (string files in Directory.GetFiles(dirs))
            {
                fileandFolderNames[counter] = files;
                counter++;
            }
            GetFilesAndFolders(dirs);
        }
        return fileandFolderNames;
    }
plinth
  • 48,267
  • 11
  • 78
  • 120
Anees
  • 885
  • 4
  • 13
  • 31
  • And a little out of theme comment, why you write 'and' from lowercase within function name? It will be more readable with uppercase, e.g. -- getFilesAndFolders. – Mikhail Churbanov Jul 13 '09 at 12:32
  • 3
    actually with standard naming conventions it should be GetFilesAndFolders() – Ian Jul 13 '09 at 12:33
  • than you should edit recursive function call too ;) I have no access to edit questions :) – Arsen Mkrtchyan Jul 13 '09 at 12:38
  • You may want to let the framework handle recursion for you by using Directory.GetFiles(path, "*", SearchOption.AllDirectories) and Directory.GetDirectories(path, "*", SearchOption.AllDirectories) – René Jul 13 '09 at 13:10

2 Answers2

3

Since copying a large folder is not a single operation, but instead consists of many separate copy operations, the FileSystemWatcher cannot determine when the copy operation of the whole folder is complete. Thus, it will give you a notification about each file separately. My suggestion is to handle each file separately. When it is copied (the event of FileSystemWatcher is triggered), calculate the MD5 and put it in the dictionary.

Vilx-
  • 104,512
  • 87
  • 279
  • 422
0

One thing you should know is that copying all the files and folders recursively is a stack overflow waiting to happen. See this question and this blog post for how to make a directory hierarchy into a non-recursive IEnumerable class.

Community
  • 1
  • 1
plinth
  • 48,267
  • 11
  • 78
  • 120