vb.net FindFirstFile 与 IO.Directory.GetFiles
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19728462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FindFirstFile with IO.Directory.GetFiles
提问by Wine Too
I have a situation where I have to find a path to the first file named my.exestarting from startingdirectory& \mydir\and go deep as needed.
Actually, IO.Directory.GetFilesis suitable but I need it stop searching after the first file is found like it is possible with FindFirstFilefrom WinAPI.
我有一种情况,我必须找到my.exe从startingdirectory&开始命名的第一个文件的路径,\mydir\并根据需要深入。
实际上,IO.Directory.GetFiles是合适的,但我需要它在找到第一个文件后停止搜索,就像FindFirstFile从 WinAPI 中可以做到的那样。
VB.NET
网络
Dim findedDirectories() As String = IO.Directory.GetFiles( _
startingdirectory & "\mydir\", "my.exe", IO.SearchOption.AllDirectories)
C#
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 stringor an empty string, not a string array? Or is here better way to search for a first file in subdirectories?
是否可以在找到第一个文件后以函数结果为 astring或 anempty string而不是 a的方式停止搜索string array?或者这里有更好的方法来搜索子目录中的第一个文件?
回答by Markus Safar
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;
}
回答by David Heffernan
I guess the simplest approach would be to organise the recursion into sub-directories yourself with recursive calls to Directory.GetDirectoriespassing SearchOption.TopDirectoryOnly. In each directory check for the file's existence with File.Exists.
我想最简单的方法是递归自己与递归调用组织成子目录Directory.GetDirectories传递SearchOption.TopDirectoryOnly。在每个目录中检查文件是否存在File.Exists。
This actually mirrors the way it would be done in Win32 with FindFirstFile. When using FindFirstFileyou always need to implement the sub-directory recursion yourself because FindFirstFilehas nothing analagous to SearchOption.AllDirectories.
这实际上反映了在 Win32 中使用FindFirstFile. 使用时,FindFirstFile您总是需要自己实现子目录递归,因为FindFirstFile与SearchOption.AllDirectories.

