Пример #1
0
        public void TraverseDirectory(int depth)
        {
            OutputWriter.WriteEmptyLine();
            int            initialIdentation = SessionData.currentPath.Split('\\').Length;
            Queue <string> subFolders        = new Queue <string>();

            subFolders.Enqueue(SessionData.currentPath);

            while (subFolders.Count != 0)
            {
                string currentPath = subFolders.Dequeue();
                int    identation  = currentPath.Split('\\').Length - initialIdentation;

                if (depth - identation < 0)
                {
                    break;
                }

                OutputWriter.WriteMessageOnNewLine(string.Format("{0}{1}",
                                                                 new string('-', identation),
                                                                 currentPath));
                try
                {
                    foreach (var file in Directory.GetFiles(currentPath))
                    {
                        int indexOfLastSlash = file.LastIndexOf("\\");
                        for (int i = 0; i < indexOfLastSlash; i++)
                        {
                            OutputWriter.WriteMessage("-");
                        }

                        OutputWriter.WriteMessageOnNewLine(file.Substring(indexOfLastSlash));
                    }

                    foreach (string directoryPath in Directory.GetDirectories(currentPath))
                    {
                        subFolders.Enqueue(directoryPath);
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    OutputWriter.DisplayException(ExceptionMessages.UnauthorizedAccessExceptionMessage);
                }
            }
        }
Пример #2
0
        public void TraverseDirectory(int depth)
        {
            //test
            if (depth == 0)
            {
                depth = 101;
            }
            else
            {
                depth--;
            }

            //endTest
            OutputWriter.WriteEmptyLine();
            int            initialIdentation = SessionData.currentPath.Split('\\').Length;
            Queue <string> subFolders        = new Queue <string>();

            subFolders.Enqueue(SessionData.currentPath);

            while (subFolders.Count != 0)
            {
                string currentPath = subFolders.Dequeue();
                int    identation  = currentPath.Split('\\').Length - initialIdentation;

                if (depth - identation < 0)
                {
                    break;
                }

                OutputWriter.WriteMessageOnNewLine($"{new string('-', identation)}{currentPath}");
                try
                {
                    foreach (var dir in Directory.GetDirectories(currentPath))
                    {
                        int indexOfLastSlash = dir.LastIndexOf("\\");
                        for (int i = 0; i < indexOfLastSlash; i++)
                        {
                            OutputWriter.WriteMessage("+");
                        }
                        var newDir = dir.Substring(indexOfLastSlash);
                        OutputWriter.WriteMessageOnNewLine(newDir);
                    }

                    foreach (var file in Directory.GetFiles(currentPath))
                    {
                        int indexOfLastSlash = file.LastIndexOf("\\");
                        for (int i = 0; i < indexOfLastSlash; i++)
                        {
                            OutputWriter.WriteMessage("-");
                        }
                        var newFile = file.Substring(indexOfLastSlash);
                        OutputWriter.WriteMessageOnNewLine(newFile);
                    }

                    foreach (string directoryPath in Directory.GetDirectories(currentPath))
                    {
                        if (depth != 101)
                        {
                            subFolders.Enqueue(directoryPath);
                        }
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    OutputWriter.DisplayException(ExceptionMessages.UnauthorizedAccessExceptionMessage);
                }
            }
        }