using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyCodeCal
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static int codeLine = 0;
static int commentLine = 0;
static int blankLine = 0;
static int all = 0;
private void btnstart_Click(object sender, EventArgs e)
{
lblshow.Text = "";
btnstart.Visible = false;
btncontinue.Visible = true;
txtBox.Visible = true;
}
private void btncontinue_Click(object sender, EventArgs e)
{
lblshow.Text = "正在统计...";
string dir = txtBox.Text;
directoryCountLine(dir);
btnresult.Visible = true;
}
static void directoryCountLine(string directory)
{
foreach (string file in Directory.GetFiles(directory, "*.cs"))
{
countLine(file);
}
}
static void countLine(string file)
{
FileStream stream = new FileStream(file, FileMode.Open);
StreamReader reader = new StreamReader(stream);
string line = reader.ReadLine();
while (line != null)
{
if (line.Trim() != "" && !(line.Trim().StartsWith(@"//")))
{
codeLine++;
}
else if (line.Trim().StartsWith(@"//"))
{
blankLine++;
}
else
{
commentLine++;
}
all = codeLine + commentLine + blankLine;
line = reader.ReadLine();
}
reader.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
lblshow.Text = "功能介绍!\n" +
"1.此程序可以统计出所在文件夹C#程序代码总行数\n" +
"2.代码行数\n" +
"3.注释行数\n" +
"4.空白行数\n";
}
private void btnresult_Click(object sender, EventArgs e)
{
btncontinue.Visible = false;
txtBox.Visible = false;
lblshow.Text = "C#代码行数为:\n"+codeLine+
"\n注释行:\n" + commentLine +
"\n空白行:\n" + blankLine +
"\n总的代码行数为:\n" + all;
}
}
}
开始界面: