我想讓我的程序讀取文件夾中包含的所有文件,然後執行所需的操作。如何使用C#讀取文件夾中存在的一個實例中的所有文件?
我曾嘗試下面的代碼,但是這是通過讀取一個文件給我的結果,然後通過文件顯示結果即文件:
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();
它工作。謝謝。 – 2013-03-06 13:10:13