统计C#项目有效代码行数的实现过程
1.实现单文件的有效行数统计
private Int64 CountTheLines(string path)
{
Int64 nowCount = 0;
try
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string str = sr.ReadLine();
while (str != null)
{
str = str.Trim();
try
{
if (str == "" || str.Substring(0, 5) == "using" || str.Substring(0, 9) == "namespace" )
nowCount--;
}
catch (Exception)
{
}
nowCount++;
str = sr.ReadLine();
}
sr.Close();
fs.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
return nowCount;
}
2.完成文件夹下有效行数的统计private void SumAllFiles(string folderpath)
{
string[] filespath = Directory.GetFiles(@folderpath);
string[] floderspath = Directory.GetDirectories(@folderpath);
Int64 Foldercount = 0; // 当前目录下的代码行号
fst = new FileStream(txt_outputFile.Text, FileMode.Append, FileAccess.Write);
swr = new StreamWriter(fst);
swr.WriteLine(DateTime.Now.ToString() + "开始计算目录: " + folderpath + " 下的有效代码行数 ");
foreach (string sttr in filespath)
{
string str = sttr.Trim().ToLower();
if (str.Substring(str.Length - 2, 2) == "cs" && str.Substring(str.Length - 11, 11) != "designer.cs")
{
Int64 count = CountTheLines(str);
swr.WriteLine("文件: " + str.Substring(folderpath.Length,str.Length - folderpath.Length) + " 的有效代码行数为: " + count.ToString() + "行");
Foldercount += count;
}
}
swr.WriteLine("目录: " + folderpath + " 下的有效代码行数为: " + Foldercount.ToString() + "行");
swr.WriteLine();
swr.WriteLine();
swr.Close();
fst.Close();
bigcount += Foldercount;
foreach (string str in floderspath)
{
SumAllFiles(str);
}
}
程序运行过程:
1.初始配置
2.执行过程
3.查看提示4.查看详细
程序下载地址:http://download.csdn.net/detail/chr23899/8435185