0

I am looking for an efficient way to search if a file exist in a given directory. The problem is I do not know the full path including all the subfolders. I only know the top level folder of the directory. Is there a way to search if a file exist in a entire directory without including the full path.

 //I would like to try this below
foreach(var file in files)
{
string filename = file.Name;
bool does_it_exist = File.Exists(C:\\,filename);
Console.WriteLine(does_it_exist);
}
  • What is the _files_ variable used in that loop? Is it a List or array of FileInfo instances? – Steve Aug 07 '19 at 20:20
  • You can use [Directory.GetFiles()](https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=netframework-4.8#System_IO_Directory_GetFiles_System_String_System_String_System_IO_SearchOption_). There's an overload that accepts a `searchOptions` parameter you can use to tell it to search subdirectories. – itsme86 Aug 07 '19 at 20:22
  • files is an array of FileInfo regarding another directory. I am checking if each file in the directory is also in another directory. – Olujide Oluade Aug 07 '19 at 20:54

1 Answers1

1

Try this:

string[] matchingFiles = System.IO.Directory.GetFiles( path, "ABC123" );