public void RemoveDirectoryBeforeRecursion()
        {
            DirectoryInfo testDirectory    = Directory.CreateDirectory(GetTestFilePath());
            DirectoryInfo testSubdirectory = Directory.CreateDirectory(Path.Combine(testDirectory.FullName, "Subdirectory"));

            using (var enumerator = new DirectoryFinishedEnumerator(testDirectory.FullName, new EnumerationOptions {
                RecurseSubdirectories = true
            }))
            {
                enumerator.DirectoryFinishedAction = (d) =>
                {
                    if (d.Equals(testDirectory.FullName, StringComparison.OrdinalIgnoreCase))
                    {
                        testSubdirectory.Delete();
                    }
                };

                // We shouldn't fail because a directory we found got deleted.
                // No errors here are possible on Windows as by the time of the deletion above, the subdirectory handle is already opened.
                while (enumerator.MoveNext())
                {
                    ;
                }
            }
        }
        public void RemoveDirectoryBeforeHandleCreationAndReplaceWithFile()
        {
            DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath());
            FileInfo      testFile      = new FileInfo(Path.Join(testDirectory.FullName, GetTestFileName()));

            testFile.Create().Dispose();
            DirectoryInfo testSubdirectory = Directory.CreateDirectory(Path.Join(testDirectory.FullName, "Subdirectory"));

            using (var enumerator = new DirectoryFinishedEnumerator(testDirectory.FullName, new EnumerationOptions {
                RecurseSubdirectories = true
            }))
            {
                // We shouldn't fail because a directory we found got deleted.
                // If replace the directory with a file when we have it's data, but BEFORE we create the handle we'll fail internally trying to create a directory handle on it.
                while (enumerator.MoveNext())
                {
                    // Using a flag file to replace the directory in the middle of processing returned data
                    if (string.Equals(enumerator.Current, testFile.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        testSubdirectory.Delete();
                        File.Create(testSubdirectory.FullName).Dispose();
                    }
                }
                ;
            }
        }
Пример #3
0
        public void RemoveDirectoryBeforeRecursion()
        {
            DirectoryInfo testDirectory    = Directory.CreateDirectory(GetTestFilePath());
            DirectoryInfo testSubdirectory = Directory.CreateDirectory(Path.Combine(testDirectory.FullName, "Subdirectory"));

            using (var enumerator = new DirectoryFinishedEnumerator(testDirectory.FullName, new EnumerationOptions {
                RecurseSubdirectories = true
            }))
            {
                enumerator.DirectoryFinishedAction = (d) => testSubdirectory.Delete();
                Assert.Throws <DirectoryNotFoundException>(() => { while (enumerator.MoveNext())
                                                                   {
                                                                       ;
                                                                   }
                                                           });
            }
        }