1

プログラムでフォルダーに含まれるすべてのファイルを読み取り、目的の操作を実行するようにします。

次のコードを試してみましたが、これは 1 つのファイルを読み取り、結果をファイルごとに表示することで結果を取得しています。

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

2 に答える 2

3

あなたの説明からは、あなたが達成しようとしていることは明確ではありません。ただし、その要点を正しく理解していれば、処理を行う前にすべてのファイルのすべての行を収集できます。

IEnumerable<string> allLinesInAllFiles
                              = Directory.GetFiles(dirPath, "*.*")
                                .Select(filePath => File.ReadLines(filePath))
                                .SelectMany(line => line);
//Now do your processing

または、統合された言語機能を使用して

IEnumerable<string> allLinesInAllFiles = 
    from filepath in Directory.GetFiles(dirPath, "*.*")
    from line in File.ReadLines(filepath)
    select line;
于 2013-03-06T12:56:48.543 に答える
1

フォルダー内のすべてのファイルのパスを取得するには、次を使用します。

string[] filePaths = Directory.GetFiles(yourPathAsString);

さらにfilePaths、このすべてのファイルに対して必要なアクションを実行するために作業できます。

于 2013-03-06T12:46:43.053 に答える