日期:2014-05-20 浏览次数:20987 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace RecorderCount { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static string strFilePath=""; public Int32 sumCount = 0; public static StreamReader mysr; public static string strFileType=""; private void btnExit_Click(object sender, EventArgs e) { this.Dispose(); Application.Exit(); } private void btnOpenFile_Click(object sender, EventArgs e) { fbdFile.ShowNewFolderButton = true; fbdFile.Description = "请选择源代码所在的文件位置"; fbdFile.ShowDialog(); if (fbdFile.SelectedPath.Length > 1) { txtFilePath.Text = fbdFile.SelectedPath.ToString(); } else { MessageBox.Show("请选择源代码所在的文件夹!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } private void btnCount_Click(object sender, EventArgs e) { if (richTextBox1.TextLength > 0) { richTextBox1.Clear(); } if (txtFileType.Text.Length == 0) { MessageBox.Show("请输入要源代码文件的类型","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information ); return; } sumCount = 0; DirectoryInfo di = new DirectoryInfo(@txtFilePath.Text.Trim()); if (cbxSubPath.Checked == true) { if (di.Exists) { int count=Info(di); richTextBox1.Text = richTextBox1.Text + "总计: " + count.ToString() + "\n\r"; richTextBox1.Text = richTextBox1.Text + "OK\n\r"; } } else { if (di.Exists) { int count=currentInfo(di); richTextBox1.Text = richTextBox1.Text + "总计: " + count.ToString() + "\n\r"; richTextBox1.Text = richTextBox1.Text + "OK\n\r"; } } } private int Info(DirectoryInfo di) { if (di.Exists) { DirectoryInfo[] di_list = di.GetDirectories(); FileInfo[] fiA=di.GetFiles(txtFileType.Text.Trim().ToString());//获得了所有起始目录下的文件 if (fiA.Length > 0) { int totalCount = 0; richTextBox1.Text=richTextBox1.Text+"统计:"+ di.FullName.ToString() + ":\n\r"; for (int i = 0; i < fiA.Length; i++) { int count = 0; StreamReader sr = new StreamReader(di.FullName.ToString() + "\\" + fiA[i].Name.ToString()); if (File.Exists(di.FullName.ToString() + "\\" + fiA[i].Name.ToString())) { string line = ""; while ((line = sr.ReadLine()) != null) { count++; } richTextBox1.Text = richTextBox1.Text + fiA[i].Name.ToString() + " " + count.ToString() + "\n\r"; totalCount += count; } } sumCount += totalCount; richTextBox1.Text=richTextBox1.Text+"小计: " + totalCount.ToString()+"\n\r"; richTextBox1.Text = richTextBox1.Text + "--------------------------------------------\n\r"; } foreach (DirectoryInfo di1 in di_list) { DirectoryInfo[] di1_list = di1.GetDirectories(); Info(di1); } } return sumCount; } private void Form1_Load(object sender, EventArgs e) { txtFileType.Text = "*.cs"; cbxSubPath.Checked = true; } private int currentInfo(DirectoryInfo di) { if (di.Exists) { DirectoryInfo[] di_list = di.GetDirectories(); FileInfo[] fiA = di.GetFiles(txtFileType.Text.Trim().ToString());//获得了所有起始目录下的文件 if (fiA.Length > 0) { int totalCount=0; richTextBox1.Text = richTextBox1.Text + di.FullName.ToString() + ":\n\r"; for (int i = 0; i < fiA.Length; i++) { int count = 0; StreamReader sr = new StreamReader(di.FullName.ToString() + "\\" + fiA[i].Name.ToString()); if (File.Exists(di.FullName.ToString() + "\\" + fiA[i].Name.ToString())) { string line = ""; while ((line = sr.ReadLine()) != null) { count++; } richTextBox1.Text = richTextBox1.Text + fiA[i].Name.ToString() + " " + count.ToString() + "\n\r"; totalCount += count; } } sumCount += totalCount; richTextBox1.Text = richTextBox1.Text + "小计: " + totalCount.ToString() + "\n\r"; richTextBox1.Text = richTextBox1.Text + "--------------------------------------------\n\r"; } } return sumCount; } } }