using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.IO ;using System.Data ;namespace LineCounter...{ /**//// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form ...{ private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1; private System.Windows.Forms.TextBox textBoxPath; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button buttonSelect; private System.Windows.Forms.Button buttonOK; private System.Windows.Forms.DataGrid dataGrid1; private System.Windows.Forms.Label labelSum; private System.Windows.Forms.TextBox textBoxExtension; private System.ComponentModel.Container components = null; [STAThread] static void Main() ...{ Application.Run(new Form1()); } public Form1() ...{ // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /**//// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) ...{ if( disposing ) ...{ if(components != null) ...{ components.Dispose(); } } base.Dispose( disposing ); } Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码 /**//// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() ...{ this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); this.textBoxPath = new System.Windows.Forms.TextBox(); this.buttonSelect = new System.Windows.Forms.Button(); this.textBoxExtension = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.buttonOK = new System.Windows.Forms.Button(); this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.labelSum = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // textBoxPath // this.textBoxPath.Location = new System.Drawing.Point(32, 24); this.textBoxPath.Name = "textBoxPath"; this.textBoxPath.ReadOnly = true; this.textBoxPath.Size = new System.Drawing.Size(440, 20); this.textBoxPath.TabIndex = 0; this.textBoxPath.Text = ""; // // buttonSelect // this.buttonSelect.Location = new System.Drawing.Point(480, 23); this.buttonSelect.Name = "buttonSelect"; this.buttonSelect.TabIndex = 1; this.buttonSelect.Text = "选择文件夹"; this.buttonSelect.Click += new System.EventHandler(this.buttonSelect_Click); // // textBoxExtension // this.textBoxExtension.Location = new System.Drawing.Point(184, 64); this.textBoxExtension.Name = "textBoxExtension"; this.textBoxExtension.Size = new System.Drawing.Size(104, 20); this.textBoxExtension.TabIndex = 3; this.textBoxExtension.Text = ".cs"; // // label2 // this.label2.Location = new System.Drawing.Point(40, 66); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(128, 16); this.label2.TabIndex = 4; this.label2.Text = "要统计的文件扩展名:"; // // buttonOK // this.buttonOK.Location = new System.Drawing.Point(320, 63); this.buttonOK.Name = "buttonOK"; this.buttonOK.Size = new System.Drawing.Size(56, 23); this.buttonOK.TabIndex = 5; this.buttonOK.Text = "开始"; this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(28, 96); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(856, 336); this.dataGrid1.TabIndex = 6; // // labelSum // this.labelSum.ForeColor = System.Drawing.Color.Red; this.labelSum.Location = new System.Drawing.Point(416, 63); this.labelSum.Name = "labelSum"; this.labelSum.Size = new System.Drawing.Size(144, 23); this.labelSum.TabIndex = 7; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(912, 454); this.Controls.Add(this.textBoxExtension); this.Controls.Add(this.textBoxPath); this.Controls.Add(this.labelSum); this.Controls.Add(this.dataGrid1); this.Controls.Add(this.buttonOK); this.Controls.Add(this.label2); this.Controls.Add(this.buttonSelect); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } #endregion System.Data.DataTable dataTable = new DataTable() ; private int Sum = 0 ; private void buttonSelect_Click(object sender, System.EventArgs e) ...{ this.folderBrowserDialog1.ShowDialog() ; this.textBoxPath.Text = this.folderBrowserDialog1.SelectedPath ; } private void buttonOK_Click(object sender, System.EventArgs e) ...{ if(this.textBoxPath.Text == "" ) ...{ return ; } this.dataTable.Clear() ; BeginCounter(this.textBoxPath.Text); this.dataGrid1.DataSource = this.dataTable ; this.labelSum.Text = "总代码数:" + Sum.ToString() ; } void BeginCounter(string path) ...{ //自动生成的代码不计算 if( path.EndsWith(@"Data" )) return ; System.IO.DirectoryInfo dir = new DirectoryInfo(path) ; System.IO.FileInfo[] subFiles = dir.GetFiles() ; // 显示当前文件夹下的行数 int Cnt = 0 ; foreach(FileInfo file in subFiles) ...{ if(this.textBoxExtension.Text.IndexOf( file.Extension) != -1) ...{ Cnt += CounterFile(file.FullName) ; } } Sum += Cnt ; DataRow row = this.dataTable.NewRow() ; row[0] = path ; row[1] = Cnt.ToString(); this.dataTable.Rows.Add(row) ; System.IO.DirectoryInfo[] subDirs = dir.GetDirectories() ; foreach(DirectoryInfo subDir in subDirs) ...{ BeginCounter(subDir.FullName) ; } } int CounterFile(string filePath) ...{ int LineCount = 0 ; // open files for streamreader StreamReader sr = File.OpenText(filePath); //loop until the end while (sr.ReadLine()!=null) ...{ LineCount++; } //close the streamreader sr.Close(); return LineCount ; } private void Form1_Load(object sender, System.EventArgs e) ...{ dataTable.Columns.Add("路径") ; dataTable.Columns.Add("行数") ; } }}