【问题标题】:FindFirstFile with IO.Directory.GetFilesFindFirstFile 与 IO.Directory.GetFiles
【发布时间】:2013-11-01 14:11:36
【问题描述】:

我有一种情况,我必须从 startingdirectory\mydir\ 开始找到名为 my.exe 的第一个文件的路径,并根据需要深入。
实际上,IO.Directory.GetFiles 是合适的,但我需要它在找到第一个文件后停止搜索,就像 WinAPI 中的 FindFirstFile 一样。

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);

是否可以在找到第一个文件后停止搜索,以使函数的结果将是stringempty string,而不是string array?或者这里有更好的方法来搜索子目录中的第一个文件?

【问题讨论】:

  • @AlexFilipovici 这并不是真正的骗局,因为我们在这里寻找目录中的单个文件。对File.Exists 的简单调用就足够了。
  • @DavidHeffernan:...根据需要深入...
  • @AlexFilipovici 好的,我想采用 SearchOption 参数的 EnumerateFiles 的重载可以满足要求。
  • 如果 DirectoryInfo 足够,我的想法是不使用递归扫描,但事实并非如此。

标签: c# vb.net


【解决方案1】:

类似以下的解决方案可能会有所帮助:

/// <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;
}

【讨论】:

    【解决方案2】:

    我想最简单的方法是通过递归调用Directory.GetDirectories 传递SearchOption.TopDirectoryOnly,自己将递归组织到子目录中。在每个目录中使用File.Exists 检查文件是否存在。

    这实际上反映了在 Win32 中使用FindFirstFile 的方式。使用FindFirstFile 时,您始终需要自己实现子目录递归,因为FindFirstFileSearchOption.AllDirectories 没有任何相似之处。

    【讨论】:

    • 那么过滤结果数组“findedDirectories”可能更容易(也可能更慢)?
    猜你喜欢
    • 2012-10-29
    • 2012-03-15
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 2017-10-09
    相关资源
    最近更新 更多