Wine Too
Wine Too

Reputation: 4655

FindFirstFile with IO.Directory.GetFiles

I have a situation where I have to find a path to the first file named my.exe starting from startingdirectory & \mydir\ and go deep as needed.
Actually, IO.Directory.GetFiles is suitable but I need it stop searching after the first file is found like it is possible with FindFirstFile from WinAPI.

VB.NET

Dim findedDirectories() As String = IO.Directory.GetFiles( _
startingdirectory & "\mydir\", "my.exe", IO.SearchOption.AllDirectories)

C#

string[] findedDirectories = IO.Directory.GetFiles( _
startingdirectory + "\\mydir\\", "my.exe", IO.SearchOption.AllDirectories);

Is it possible to stop searching after the first file is found in a way that the result of the function will be a string or an empty string, not a string array? Or is here better way to search for a first file in subdirectories?

Upvotes: 1

Views: 9890

Answers (2)

Markus Safar
Markus Safar

Reputation: 6580

A solution like the following one could help:

/// <summary>
/// Searches for the first file matching to searchPattern in the sepcified path.
/// </summary>
/// <param name="path">The path from where to start the search.</param>
/// <param name="searchPattern">The pattern for which files to search for.</param>
/// <returns>Either the complete path including filename of the first file found
/// or string.Empty if no matching file could be found.</returns>
public static string FindFirstFile(string path, string searchPattern)
{
    string[] files;

    try
    {
        // Exception could occur due to insufficient permission.
        files = Directory.GetFiles(path, searchPattern, SearchOption.TopDirectoryOnly);
    }
    catch (Exception)
    {
        return string.Empty;
    }

    // If matching files have been found, return the first one.
    if (files.Length > 0)
    {
        return files[0];
    }
    else
    {
        // Otherwise find all directories.
        string[] directories;

        try
        {
            // Exception could occur due to insufficient permission.
            directories = Directory.GetDirectories(path);
        }
        catch (Exception)
        {
            return string.Empty;
        }

        // Iterate through each directory and call the method recursivly.
        foreach (string directory in directories)
        {
            string file = FindFirstFile(directory, searchPattern);

            // If we found a file, return it (and break the recursion).
            if (file != string.Empty)
            {
                return file;
            }
        }
    }

    // If no file was found (neither in this directory nor in the child directories)
    // simply return string.Empty.
    return string.Empty;
}

Upvotes: 4

David Heffernan
David Heffernan

Reputation: 612854

I guess the simplest approach would be to organise the recursion into sub-directories yourself with recursive calls to Directory.GetDirectories passing SearchOption.TopDirectoryOnly. In each directory check for the file's existence with File.Exists.

This actually mirrors the way it would be done in Win32 with FindFirstFile. When using FindFirstFile you always need to implement the sub-directory recursion yourself because FindFirstFile has nothing analagous to SearchOption.AllDirectories.

Upvotes: 2

Related Questions