Example #1
0
        /// <summary>
        /// 访问者模式相关
        /// </summary>
        /// <param name="visitor"></param>
        public void Accept(IFileVisitor visitor)
        {
            foreach (var item in this._childList)
            {
                item.Accept(visitor);
            }

            visitor.Visit(this);
        }
Example #2
0
        /// <summary>
        /// Walk through a directory tree start with a root file, and performs <see cref="IFileVisitor.Visit"/> on each walked file
        /// </summary>
        /// <param name="fileSystemInfo">the root file</param>
        /// <param name="visitor">a visitor pattern</param>
        /// <exception cref="DirectoryNotFoundException"></exception>
        /// <exception cref="SecurityException"></exception>
        /// <exception cref="UnauthorizedAccessException"></exception>
        public static void WalkFileTree(this FileSystemInfo fileSystemInfo, IFileVisitor visitor)
        {
            switch (fileSystemInfo)
            {
            case FileInfo file:
                visitor.Visit(file);
                return;

            case DirectoryInfo directoryInfo:
            {
                // walk thru directories
                var dirRemains = new Stack <DirectoryInfo>();
                if (!directoryInfo.Exists)
                {
                    throw new DirectoryNotFoundException(directoryInfo.FullName);
                }
                dirRemains.Push(directoryInfo);
                while (dirRemains.Any())
                {
                    var toWalk = dirRemains.Pop();
                    foreach (var fileInfo in toWalk.GetFiles())
                    {
                        visitor.Visit(fileInfo);
                    }

                    foreach (var subDirectory in toWalk.GetDirectories())
                    {
                        dirRemains.Push(subDirectory);
                    }
                }

                return;
            }

            default:
                throw new ArgumentException($"parameter {nameof(fileSystemInfo)} must be one of {{{nameof(FileInfo)}, {nameof(DirectoryInfo)}}}");
            }
        }
Example #3
0
 /// <summary>
 /// 访问者模式相关
 /// </summary>
 /// <param name="visitor"></param>
 public void Accept(IFileVisitor visitor)
 {
     visitor.Visit(this);
 }