コード例 #1
0
        private static bool recursiveDelete(string testName, FileSystemInfo fs, bool silent,  bool failOnError)
        {
            Debug.Assert(!(silent && failOnError));

            if (fs.IsFile())
            {
                fs.DeleteFile();
                return silent;
            }

            var dir = new DirectoryInfo(fs.FullName);
            if (!dir.Exists) return silent;

            try
            {
                FileSystemInfo[] ls = dir.GetFileSystemInfos();

                foreach (FileSystemInfo e in ls)
                {
                    silent = recursiveDelete(testName, e, silent, failOnError);
                }

                dir.Delete();
            }
            catch (IOException e)
            {
                ReportDeleteFailure(testName, failOnError, fs, e.Message);
            }

            return silent;
        }
コード例 #2
0
        /// <summary>
        /// Utility method to delete a directory recursively. It is
        /// also used internally. If a file or directory cannot be removed
        /// it throws an AssertionFailure.
        /// </summary>
        /// <param name="fs"></param>
        /// <param name="silent"></param>
        /// <param name="name"></param>
        /// <param name="failOnError"></param>
        /// <returns></returns>
        protected static bool recursiveDelete(FileSystemInfo fs, bool silent, string name, bool failOnError)
        {
            Debug.Assert(!(silent && failOnError));

            if (fs.IsFile())
            {
                fs.DeleteFile();
                return silent;
            }

            var dir = new DirectoryInfo(fs.FullName);
            if (!dir.Exists) return silent;

            try
            {
                FileSystemInfo[] ls = dir.GetFileSystemInfos();

                foreach (FileSystemInfo e in ls)
                {
                    silent = recursiveDelete(e, silent, name, failOnError);
                }

                dir.Delete();
            }
            catch (IOException e)
            {
                //ReportDeleteFailure(name, failOnError, fs);
                Console.WriteLine(name + ": " + e.Message);
            }

            return silent;
        }
コード例 #3
0
        private static void recursiveDelete(string testName, FileSystemInfo fs,  bool failOnError)
        {
            if (fs.IsFile())
            {
                if (!fs.DeleteFile())
                {
                    throw new IOException("Unable to delete file: " + fs.FullName);
                }

                // Deleting a file only marks it for deletion, following code blocks until
                // the file is actually deleted. For a more thorough explanation see the
                // comment below @ the directory.Delete()
                while (File.Exists(fs.FullName))
                {
                    Thread.Sleep(0);
                }
                return;
            }

            var dir = new DirectoryInfo(fs.FullName);
            if (!Directory.Exists(dir.FullName)) return;

            try
            {
                FileSystemInfo[] ls = dir.GetFileSystemInfos();

                foreach (FileSystemInfo e in ls)
                {
                    recursiveDelete(testName, e, failOnError);
                }
                
                dir.Delete();

                // dir.Delete() only marks the directory for deletion (see also: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/a2fcc569-1835-471f-b731-3fe9c6bcd2d9),
                // so it creates a race condition between the actual deletion of this directory, and the 
                // deletion of the parent directory. If this directory is not deleted when the parent directory
                // is tried to be deleted, an IOException ("Directory not empty") will be thrown.
                // The following code blocks untill the directory is actually deleted.
                // (btw, dir.Exists keeps returning true on my (Windows 7) machine, even if the Explorer and Command
                // say otherwise)
                while (Directory.Exists(dir.FullName))
                {
                    Thread.Sleep(0);
                }
            }
            catch (IOException ex)
            {
                ReportDeleteFailure(testName, failOnError, fs, ex.ToString());
            }
        }