如何最小化文件搜索运行时的复杂性?

时间:2019-05-03 12:26:13

标签: c# .net-core

我写了一个应用程序,它是一个自定义控制台,允许执行各种命令。其中一个命令允许根据文件名的一部分找到文件的完整路径。输入数据是一个字符串,它等于文件的部分\全名。

我的问题是-如何尽可能减少搜索代码的运行时复杂度?

这是命令的代码:

The file found in path: c:\temp\test.json
The file found in path: c:\temp\test.json
The file found in path: c:\temp\test.xml
The file found in path: c:\temp\test.json
The file found in path: c:\temp\test.xml
The file found in path: c:\temp\test\test.json

示例-对于输入参数“ c:\ temp test”,输出可以是:

sys.displayhook

1 个答案:

答案 0 :(得分:2)

您可以像这样简单地访问您的foreach

var fileLocations  =  Directory.GetFiles(initialLocation, $"{filePath}.*", SearchOption.A­llDirectories);

 foreach (var location in fileLocations)
 {
       returnedOutput += $"The file found in path: {location}\n";
       Console.Write(returnedOutput);
 }

其余代码也可以简化。

相关问题