我正在编写一个简单的程序,我正在尝试通读文本文件并计算特定短语的出现次数。这些文本文件实际上是我服务器的日志文件,但无论如何包含我的程序的文件夹结构就像
FailedReqLogFiles
....
LogFiles
folder1
textFile1
textFile2
folder2
textFile3
folder3
textFile4
textFile5
textFile6
wmsvg
....
MyProgram.exe
我在跑
C:\inetpub\logs\MyProgram.exe "LogFiles" "somephrase"
从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);
}
}
}
我无法弄清楚为什么它没有找到文件,因为它一直打印"length of folder: 0"
我的程序有任何明显的问题吗?
答案 0 :(得分:4)
你正在调用string [] folders = System.IO.Directory.GetFiles(args [0]);
您是否尝试过Directory.GetDirectories?
https://msdn.microsoft.com/en-us/library/c1sez4sc(v=vs.110).aspx