Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- c# find a FOLDER when path is unknown then list files and return
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace listFoldersTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.SetWindowSize(100, 50);
- DirectoryInfo dir = new DirectoryInfo(@"C:UsersusernameMusic");
- getDirsFiles(dir, 0, 2);
- Console.ReadKey();
- Console.WriteLine("done");
- }
- public static void getDirsFiles(DirectoryInfo d, int currentDepth, int maxDepth)
- {
- String folderToFindName = ("albumName");
- bool foundIt = false;
- if (currentDepth < maxDepth)
- {
- DirectoryInfo[] dirs = d.GetDirectories("*.*");
- foreach (DirectoryInfo dir in dirs)
- {
- String pathName = (dir.FullName);
- Console.WriteLine("r{0} ", dir.Name);
- if (currentDepth == (maxDepth - 1))
- {
- if (pathName.IndexOf(folderToFindName) != -1)
- {
- foundIt = true;
- FileInfo[] files = dir.GetFiles("*.*");
- foreach (FileInfo file in files)
- {
- Console.WriteLine("-------------------->> {0} ", file.Name);
- } //end foreach files
- } // end if pathName
- } // end if of get files current depth
- if (foundIt == true)
- {
- return;
- }
- getDirsFiles(dir, currentDepth + 1, maxDepth);
- } //end if foreach directories
- } //end if directories current depth
- } // end getDirsFiles function
- }
- }
- using System;
- using System.IO;
- namespace listFoldersTest
- {
- class Program
- {
- private static bool foundIt;
- static void Main(string[] args)
- {
- Console.SetWindowSize(100, 50);
- try
- {
- DirectoryInfo dir = new DirectoryInfo(args[0]);
- getDirsFiles(dir, 0, 2);
- }
- catch
- {
- }
- Console.ReadKey();
- Console.WriteLine("done");
- }
- public static void getDirsFiles(DirectoryInfo d, int currentDepth, int maxDepth)
- {
- if(d == null || foundIt) return;
- String folderToFindName = ("albumName");
- if (currentDepth < maxDepth)
- {
- DirectoryInfo[] dirs = d.GetDirectories("*.*");
- foreach (DirectoryInfo dir in dirs)
- {
- String pathName = (dir.FullName);
- Console.WriteLine("r{0} ", dir.Name);
- if (currentDepth == (maxDepth - 1))
- {
- if (pathName.IndexOf(folderToFindName) != -1)
- {
- foundIt = true;
- FileInfo[] files = dir.GetFiles("*.*");
- foreach (FileInfo file in files)
- {
- Console.WriteLine("-------------------->> {0} ", file.Name);
- } //end foreach files
- return;
- } // end if pathName
- } // end if of get files current depth
- getDirsFiles(dir, currentDepth + 1, maxDepth);
- } //end if foreach directories
- } //end if directories current depth
- } // end getDirsFiles function
- }
- }
- public static string FindFolder(DirectoryInfo rootDirectory, string folderToFind, int currentDepth, int maxDepth)
- {
- if(currentDepth == maxDepth)
- {
- return null;
- }
- foreach(var directory in rootDirectory.GetDirectories())
- {
- Console.WriteLine(directory.FullName);
- if(directory.Name.Equals(folderToFind,StringComparison.OrdinalIgnoreCase))
- {
- return directory.FullName;
- }
- string tempFindResult;
- if((tempFindResult = FindFolder(directory,folderToFind,++currentDepth,maxDepth)) != null)
- {
- return tempFindResult;
- }
- }
- return null;
- }
- if (foundIt == true)
- {
- break; // instead of return
- }
Add Comment
Please, Sign In to add comment