无法弄清楚为什么GetFiles(...)不适合我

时间:2022-06-01 21:09:54

I'm making a simple program where I'm trying to read through text files and count the number of occurrences of a specific phrase. These text files are in fact log files for my server, but anyhow the folder structure including my program is like

我正在制作一个简单的程序,我试图通读文本文件并计算特定短语的出现次数。这些文本文件实际上是我服务器的日志文件,但无论如何包含我的程序的文件夹结构都是这样的

FailedReqLogFiles
    ....
LogFiles
    folder1
         textFile1
         textFile2
    folder2
         textFile3
    folder3
         textFile4
         textFile5
         textFile6
wmsvg
    ....
MyProgram.exe 

and I'm running

而且我正在跑步

C:\inetpub\logs\MyProgram.exe "LogFiles" "somephrase"

from the Windows command line. The entire source code for my program is

从Windows命令行。我的程序的完整源代码是

using System;
using System.IO;
using System.Text.RegularExpressions;


class Test
{
    static void Main(string[] args)
    {
    // args[1] = expression to search for, e.g. "cloudrealized-email-top-banner"
        try
        {
            int count = 0;
            string [] folders = System.IO.Directory.GetFiles(args[0]); // LogFiles subfolders, e.g. {"W3SVC1", "W3SVC3", "W3SVC5" , ... }
            Console.Write("length of folder: {0}\n", folders.Length);
            foreach (string thisfolder in folders)
            {
                string[] logs = System.IO.Directory.GetFiles(thisfolder);
                foreach (string thislog in logs)
                {
                    using (StreamReader sr = new StreamReader(args[0]))
                    {
                        String line = sr.ReadToEnd();
                        for (Match m = Regex.Match(line, args[1]); m.Success; m = m.NextMatch()) ++count;
                    }
                }
            }
            Console.WriteLine("{0} currences of {1} found in log files", count, args[1]);
        }
        catch (Exception e)
        {
            Console.WriteLine("Error occured");
            Console.WriteLine(e.Message);
        }
    }
}

and I can't figure out why it's not finding the files, as it keeps printing "length of folder: 0"

我无法弄清楚为什么它没有找到文件,因为它不断打印“文件夹的长度:0”

Is there any glaring problem with my program?

我的程序有任何明显的问题吗?

1 个解决方案

#1


You're calling string [] folders = System.IO.Directory.GetFiles(args[0]);

你正在调用string [] folders = System.IO.Directory.GetFiles(args [0]);

Have you instead tried Directory.GetDirectories?

您是否尝试过Directory.GetDirectories?

https://msdn.microsoft.com/en-us/library/c1sez4sc(v=vs.110).aspx

#1


You're calling string [] folders = System.IO.Directory.GetFiles(args[0]);

你正在调用string [] folders = System.IO.Directory.GetFiles(args [0]);

Have you instead tried Directory.GetDirectories?

您是否尝试过Directory.GetDirectories?

https://msdn.microsoft.com/en-us/library/c1sez4sc(v=vs.110).aspx