fork download
  1. public class StackBasedIteration
  2. {
  3. static void Main(string[] args)
  4. {
  5. TraverseTree(args[0]);
  6.  
  7. Console.WriteLine("Press key");
  8. Console.ReadKey();
  9. }
  10.  
  11. public static void TraverseTree(string root)
  12. {
  13. Stack<string> dirs = new Stack<string>(20);
  14.  
  15. if (!System.IO.Directory.Exists(root))
  16. {
  17. throw new ArgumentException();
  18. }
  19. dirs.Push(root);
  20.  
  21. while (dirs.Count > 0)
  22. {
  23. string currentDir = dirs.Pop();
  24. string[] subDirs;
  25. try
  26. {
  27. subDirs = System.IO.Directory.GetDirectories(currentDir);
  28. }
  29. catch (UnauthorizedAccessException e)
  30. {
  31. Console.WriteLine(e.Message);
  32. continue;
  33. }
  34. catch (System.IO.DirectoryNotFoundException e)
  35. {
  36. Console.WriteLine(e.Message);
  37. continue;
  38. }
  39.  
  40. string[] files = null;
  41. try
  42. {
  43. files = System.IO.Directory.GetFiles(currentDir);
  44. }
  45.  
  46. catch (UnauthorizedAccessException e)
  47. {
  48.  
  49. Console.WriteLine(e.Message);
  50. continue;
  51. }
  52.  
  53. catch (System.IO.DirectoryNotFoundException e)
  54. {
  55. Console.WriteLine(e.Message);
  56. continue;
  57. }
  58. foreach (string file in files)
  59. {
  60. try
  61. {
  62. System.IO.FileInfo fi = new System.IO.FileInfo(file);
  63. Console.WriteLine("{0}: {1}, {2}", fi.Name, fi.Length, fi.CreationTime);
  64. }
  65. catch (System.IO.FileNotFoundException e)
  66. {
  67. Console.WriteLine(e.Message);
  68. continue;
  69. }
  70. }
  71. foreach (string str in subDirs)
  72. dirs.Push(str);
  73. }
  74. }
  75. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(7,9): error CS0103: The name `Console' does not exist in the current context
prog.cs(8,9): error CS0103: The name `Console' does not exist in the current context
prog.cs(13,9): error CS0246: The type or namespace name `Stack' could not be found. Are you missing `System.Collections.Generic' using directive?
prog.cs(17,23): error CS0246: The type or namespace name `ArgumentException' could not be found. Are you missing `System' using directive?
prog.cs(19,9): error CS0841: A local variable `dirs' cannot be used before it is declared
prog.cs(21,16): error CS0841: A local variable `dirs' cannot be used before it is declared
prog.cs(23,33): error CS0841: A local variable `dirs' cannot be used before it is declared
prog.cs(29,20): error CS0246: The type or namespace name `UnauthorizedAccessException' could not be found. Are you missing `System' using directive?
prog.cs(36,17): error CS0103: The name `Console' does not exist in the current context
prog.cs(46,20): error CS0246: The type or namespace name `UnauthorizedAccessException' could not be found. Are you missing `System' using directive?
prog.cs(55,17): error CS0103: The name `Console' does not exist in the current context
prog.cs(63,21): error CS0103: The name `Console' does not exist in the current context
prog.cs(67,21): error CS0103: The name `Console' does not exist in the current context
prog.cs(72,17): error CS0841: A local variable `dirs' cannot be used before it is declared
Compilation failed: 14 error(s), 0 warnings
stdout
Standard output is empty