last year i started programming on my own, so far all i have been is simple, but i hit a problem with the code to look for a file in a location.
what i want the code to do is give all the files that meet the requirements in the given directory and subdirectory's.
but all i get is the results of the main folder and the first subdirectory. What i am doing wrong.
using System;
using System.IO;
public class FileSearch
{
public static void Main()
{
Console.WriteLine("input name or part of the name.");
string userinput1 = Console.ReadLine();
Console.WriteLine("input location to search");
string userinput2 = Console.ReadLine();
String searchName = userinput1;
DirectoryInfo myDir = new DirectoryInfo(@userinput2);
SearchDirectories(myDir, searchName);
}
public static void SearchDirectories(DirectoryInfo dir, String target)
{
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
if (file.Name.IndexOf(target) > -1)
{
Console.WriteLine(file.Name);
}
}
DirectoryInfo[] dirs = dir.GetDirectories();
foreach (DirectoryInfo subDir in dirs)
{
SearchDirectories(subDir, target);
}
Console.WriteLine("if you want to exit the program type close.");
string close = Console.ReadLine();
if (close.ToLower() == "close") { return; }
else { Main();
Console.Clear();
}
}
}

New Topic/Question
Reply




MultiQuote




|