using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace form
{
public partial class Form1 : Form
{
int files_num = 0;
int all_lines = 0;
int rem_lines = 0;
int space_lines = 0;
int except_file_num = 0;
string file_ext = ".txt|.cs|.aspx|.html|.htm";
Thread work;
public Form1()
{
InitializeComponent();
//Control.CheckForIllegalCrossThreadCalls = false;
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult result=this.folderBrowserDialog1.ShowDialog();
this.textBox3.Text = this.folderBrowserDialog1.SelectedPath;
}
private void button1_Click(object sender, EventArgs e)
{
files_num = 0;
all_lines = 0;
rem_lines = 0;
space_lines = 0;
except_file_num = 0;
this.progressBar1.Value = 0;
if (this.textBox3.Text == "")
{
MessageBox.Show("请先指定项目目录!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
work = new Thread(new ThreadStart(StartWork));
work.Start();
}
private void StartWork()
{
MethodInvoker mi = new MethodInvoker(Thread_Proc);
BeginInvoke(mi);
}
private void Thread_Proc()
{
//统计文件数
CountFiles(this.textBox3.Text);
this.progressBar1.Minimum = 0;
this.progressBar1.Maximum = files_num;
CountLines(this.textBox3.Text);
}
private void CountFiles(string folderPath)
{
DirectoryInfo selected_dir = new DirectoryInfo(folderPath);
if (!selected_dir.Exists)
return;
FileInfo[] files = selected_dir.GetFiles();
foreach (var item in files)
{
files_num++;
}
foreach (var item in selected_dir.GetDirectories())
{
CountFiles(folderPath + "\\" + item.Name);
}
}
private void CountLines(string folderPath)
{
DirectoryInfo selected_dir = new DirectoryInfo(folderPath);
if (!selected_dir.Exists)
return;
FileInfo[] files = selected_dir.GetFiles();
foreach(var file in files)
{
try
{
SetLabelValue3(folderPath+"\\"+file.Name);
//判断扩展名
string[] ext=file_ext.Split('|');
string cur_ext=file.Extension;
bool find=false;
foreach(var item in ext)
{
if (cur_ext == item)
{
find = true;
break;
}
}
if(!find)
{
except_file_num++;
this.progressBar1.Value++;
Thread.Sleep(100);
continue;
}
string[] lines = File.ReadAllLines(folderPath+"\\"+file.Name);
CountRems(lines);
foreach (var item in lines)
{
string tmp_item = item.Trim();
if (tmp_item == "")
{
space_lines++;
SetLabelValue4(space_lines.ToString());
}
all_lines++;
SetLabelValue1(all_lines.ToString());
}
}
catch (Exception ex)
{
//以防权限等错误,本版本忽略掉不能按行读取的文件
MessageBox.Show(ex.Message);
}
this.progressBar1.Value++;
Thread.Sleep(100);
}
foreach (var item in selected_dir.GetDirectories())
{
CountLines(folderPath+"\\"+item.Name);
}
SetLabelValue3("完成!存在" + except_file_num.ToString() + "个非源码文件!");
}
void CountRems(string[] lines)
{
//寻找 "//" 注释行
foreach (var item in lines)
{
string tmp_item = item.Trim();
if (tmp_item.Length > 2 && tmp_item.Substring(0, 2) == "//")
{
rem_lines++;
SetLabelValue2(rem_lines.ToString());
}
}
int rem_span = 0;
bool findhead=false;
//寻找 "/*……*/" 注释块
foreach (var item in lines)
{
string tmp_item = item.Trim();
if (tmp_item.Length > 2 && tmp_item.Substring(0, 2) == "/*")
{
findhead = true;
}
if (tmp_item.Length > 2 && tmp_item.Substring(0, 2) == "*/")
{
findhead = false;
}
if (findhead)
{
rem_span++;
rem_lines++;
}
}
}
delegate void SetLabelCallback(Label lb,string text);
delegate void SetTextCallback(string text);
void SetLabelValue(Label lb, string text)
{
if (lb.InvokeRequired)
{
SetLabelCallback d = new SetLabelCallback(SetLabelValue);
lb.Invoke(d, new object[] { text });
}
else
{
lb.Text = text;
}
lb.Refresh();
Application.DoEvents();
}
void SetLabelValue1(string text)
{
if (this.label5.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetLabelValue1);
this.label5.Invoke(d, new object[] { text });
}
else
{
this.label5.Text = text;
}
this.label5.Refresh();
Application.DoEvents();
}
void SetLabelValue2(string text)
{
if (this.label6.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetLabelValue2);
this.label6.Invoke(d, new object[] { text });
}
else
{
this.label6.Text = text;
}
this.label6.Refresh();
Application.DoEvents();
}
void SetLabelValue3(string text)
{
if (this.label7.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetLabelValue3);
this.label7.Invoke(d, new object[] { text });
}
else
{
this.label7.Text = text;
}
this.label7.Refresh();
Application.DoEvents();
}
void SetLabelValue4(string text)
{
if (this.label9.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetLabelValue4);
this.label9.Invoke(d, new object[] { text });
}
else
{
this.label9.Text = text;
}
this.label9.Refresh();
Application.DoEvents();
}
void DelegateSetValue(string text)
{
this.label5.Text = text;
this.label6.Text = text;
this.label7.Text = text;
this.label9.Text = text;
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.linkLabel1.Links[0].LinkData = "http://www.cnzui.com";
System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}
}
}