Guest User

Untitled

a guest
Aug 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. c# find a FOLDER when path is unknown then list files and return
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7.  
  8. namespace listFoldersTest
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. Console.SetWindowSize(100, 50);
  15. DirectoryInfo dir = new DirectoryInfo(@"C:UsersusernameMusic");
  16. getDirsFiles(dir, 0, 2);
  17. Console.ReadKey();
  18. Console.WriteLine("done");
  19. }
  20. public static void getDirsFiles(DirectoryInfo d, int currentDepth, int maxDepth)
  21. {
  22. String folderToFindName = ("albumName");
  23. bool foundIt = false;
  24.  
  25. if (currentDepth < maxDepth)
  26. {
  27. DirectoryInfo[] dirs = d.GetDirectories("*.*");
  28. foreach (DirectoryInfo dir in dirs)
  29. {
  30. String pathName = (dir.FullName);
  31. Console.WriteLine("r{0} ", dir.Name);
  32. if (currentDepth == (maxDepth - 1))
  33. {
  34. if (pathName.IndexOf(folderToFindName) != -1)
  35. {
  36. foundIt = true;
  37. FileInfo[] files = dir.GetFiles("*.*");
  38. foreach (FileInfo file in files)
  39. {
  40. Console.WriteLine("-------------------->> {0} ", file.Name);
  41. } //end foreach files
  42.  
  43. } // end if pathName
  44.  
  45. } // end if of get files current depth
  46.  
  47. if (foundIt == true)
  48. {
  49. return;
  50. }
  51. getDirsFiles(dir, currentDepth + 1, maxDepth);
  52.  
  53. } //end if foreach directories
  54.  
  55. } //end if directories current depth
  56.  
  57. } // end getDirsFiles function
  58. }
  59. }
  60.  
  61. using System;
  62. using System.IO;
  63.  
  64. namespace listFoldersTest
  65. {
  66. class Program
  67. {
  68. private static bool foundIt;
  69.  
  70. static void Main(string[] args)
  71. {
  72. Console.SetWindowSize(100, 50);
  73. try
  74. {
  75. DirectoryInfo dir = new DirectoryInfo(args[0]);
  76. getDirsFiles(dir, 0, 2);
  77. }
  78. catch
  79. {
  80. }
  81. Console.ReadKey();
  82. Console.WriteLine("done");
  83. }
  84.  
  85. public static void getDirsFiles(DirectoryInfo d, int currentDepth, int maxDepth)
  86. {
  87. if(d == null || foundIt) return;
  88. String folderToFindName = ("albumName");
  89. if (currentDepth < maxDepth)
  90. {
  91. DirectoryInfo[] dirs = d.GetDirectories("*.*");
  92. foreach (DirectoryInfo dir in dirs)
  93. {
  94. String pathName = (dir.FullName);
  95. Console.WriteLine("r{0} ", dir.Name);
  96. if (currentDepth == (maxDepth - 1))
  97. {
  98. if (pathName.IndexOf(folderToFindName) != -1)
  99. {
  100. foundIt = true;
  101. FileInfo[] files = dir.GetFiles("*.*");
  102. foreach (FileInfo file in files)
  103. {
  104. Console.WriteLine("-------------------->> {0} ", file.Name);
  105. } //end foreach files
  106. return;
  107. } // end if pathName
  108. } // end if of get files current depth
  109. getDirsFiles(dir, currentDepth + 1, maxDepth);
  110. } //end if foreach directories
  111. } //end if directories current depth
  112. } // end getDirsFiles function
  113. }
  114. }
  115.  
  116. public static string FindFolder(DirectoryInfo rootDirectory, string folderToFind, int currentDepth, int maxDepth)
  117. {
  118. if(currentDepth == maxDepth)
  119. {
  120. return null;
  121. }
  122.  
  123. foreach(var directory in rootDirectory.GetDirectories())
  124. {
  125. Console.WriteLine(directory.FullName);
  126.  
  127. if(directory.Name.Equals(folderToFind,StringComparison.OrdinalIgnoreCase))
  128. {
  129. return directory.FullName;
  130. }
  131.  
  132. string tempFindResult;
  133.  
  134. if((tempFindResult = FindFolder(directory,folderToFind,++currentDepth,maxDepth)) != null)
  135. {
  136. return tempFindResult;
  137. }
  138. }
  139.  
  140. return null;
  141. }
  142.  
  143. if (foundIt == true)
  144. {
  145. break; // instead of return
  146. }
Add Comment
Please, Sign In to add comment