Find the string within List<string>
Hi,I have load all the files from folder and it's sub folder and kept in List<string>. I want to search the file name in this List<string> how can i do this from button click(button6). below is the code..
private void button5_Click(object sender, EventArgs e)
{
List<string> allFiles = GettingFiles(@"c:\");
}
private void button6_Click(object sender, EventArgs e)
{
}
public static List<string> GettingFiles(string path)
{
List<string> listFiles = new List<string>();
//1. get files from the current directory:
string[] currentFiles = Directory.GetFiles(path, "*.*");
foreach (string file in currentFiles)
listFiles.Add(file);
//2. get files from other sub directories:
string[] directories = Directory.GetDirectories(path);
foreach (string dir in directories)
{
string[] files = GetFilesFromDirectory(dir);
listFiles.AddRange(files);
}
//for the end, lets get only the names of the files (remove the path):
for (int i = 0; i < listFiles.Count; i++)
{
string fileName = Path.GetFileName(listFiles[i]);
listFiles[i] = fileName;
}
return listFiles;
}
private static string[] GetFilesFromDirectory(string path)
{
string[] files = Directory.GetFiles(path);
return files;
}