static void Main(string[] args)
{
//获取文件目录
string[] files = Directory.GetFiles("D:\\传智播客\\我的播放器-情意中国风\\我的播放器-情意中国风");
//遍历每一个文件
foreach (string f in files)
{
//选择.cs结尾的文件
if (Regex.IsMatch(f, "\\.cs$"))
{
Console.WriteLine(f);//输出文件名
count(f);
}
}
Console.WriteLine("代码行:" + codeLine);
Console.WriteLine("注释行:" + notesLine);
Console.WriteLine("空白行:" + whiteLine);
Console.ReadKey();
}
public static int codeLine = 0;//代码行数,初值0
public static int notesLine = 0;//注释行数,初值0
public static int whiteLine = 0;//空白行数,初值0
static void count(string path)
{
StreamReader sr = null;
sr = new StreamReader(path, Encoding.Default);
Boolean bo = true;//标记
string patt = Regex.Escape("*/");
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (bo)
{
if (line.Length==0)//网搜的正则都不匹配,改为长度检查
{
whiteLine++;
}
else if (Regex.IsMatch(line, "//"))
{
notesLine++;
}
else if (line.StartsWith("/*"))
{
notesLine++;
if (!line.EndsWith("*/"))
{
/*
*/
bo = false;
}
}
else
{
codeLine++;
}
}
else
{
if (line.EndsWith("*/"))
{
bo = true;
}
notesLine++;
}
}
}
|