我希望我的程序读取文件夹中包含的所有文件,然后执行所需的操作。
我尝试了以下代码,但这是通过读取一个文件然后显示结果,即逐个文件显示结果给我的结果:
foreach (string file in Directory.EnumerateFiles(@"C:\Users\karansha\Desktop\Statistics\Transfer", "*.*", SearchOption.AllDirectories))
{
Console.WriteLine(file);
System.IO.StreamReader myFile = new System.IO.StreamReader(file);
string searchKeyword = "WX Search";
string[] textLines = File.ReadAllLines(file);
Regex regex = new Regex(@"Elapsed Time:\s*(?<value>\d+\.?\d*)\s*ms");
double totalTime = 0;
int count = 0;
foreach (string line in textLines)
{
if (line.Contains(searchKeyword))
{
Match match = regex.Match(line);
if (match.Captures.Count > 0)
{
try
{
count++;
double time = Double.Parse(match.Groups["value"].Value);
totalTime += time;
}
catch (Exception)
{
// no number
}
}
}
}
double average = totalTime / count;
Console.WriteLine("RuleAverage=" + average);
// keep screen from going away
// when run from VS.NET
Console.ReadLine();