看到一坨坨的代码,实在不能继续下去,有个想法,统计一下这个工程的代码量,估计一下自己的工作量。最后,很失望。
本文代码只是用作统计*.cs文件,设计文件不包括。
12.19
1.增加文件夹拖放功能
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;
namespace TextLineCounter
{
public partial class mainFrm : Form
{
public mainFrm()
{
InitializeComponent();
}
private void btnCheck_Click(object sender, EventArgs e)
{
string folderPath = this.textBoxPath.Text;
if (folderPath == "")
{
FolderBrowserDialog folderDlg = new FolderBrowserDialog();
folderDlg.ShowDialog();
folderPath = folderDlg.SelectedPath;
this.textBoxPath.Text = folderPath;
}
//存放符合条件的文件名
List<string> fileList = new List<string>();
if (folderPath != "")
{
//目录信息
DirectoryInfo theFolder = new DirectoryInfo(folderPath);
//文件
foreach (FileInfo NextFile in theFolder.GetFiles())
{
string fileName = NextFile.Name.ToString();
int csLocate = fileName.LastIndexOf(".cs");
int designLocate = fileName.LastIndexOf(".D");
int cspLocate = fileName.LastIndexOf(".csp");
//MessageBox.Show(designLocate + "-" + csLocate.ToString() + "/" + fileName);
if (designLocate < 0 && cspLocate < 0 && csLocate > 0)
{
fileList.Add(fileName);
}
}
int[] LineArray = new int[fileList.Count];
for (int i = 0; i < fileList.Count; i++)
{
string path = folderPath + @"\" + fileList[i];
//MessageBox.Show(path);
LineArray[i] = CountLines(path);
}
int sum = 0;
for (int j = 0; j < LineArray.Length; j++)
{
sum += LineArray[j];
//MessageBox.Show(LineArray[j].ToString());
}
MessageBox.Show(sum.ToString());
}
}
/// <summary>
/// 根据文件路径统计文件的行数
/// </summary>
/// <param name="path">路径</param>
/// <returns></returns>
public static int CountLines(string path)
{
//string path = @"c:\temp\MyTest.txt";
int lineCount = 0;
try
{
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
sr.ReadLine();
lineCount++;
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return lineCount;
}
private void textBoxPath_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
this.textBoxPath.Cursor = System.Windows.Forms.Cursors.Arrow;//指定鼠标形状(更好看)
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void textBoxPath_DragDrop(object sender, DragEventArgs e)
{
//DataFormats 数据的格式,下有多个静态属性都为string型,除FileDrop格式外还有Bitmap,Text,WaveAudio等格式
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
this.textBoxPath.Text = path;
this.textBoxPath.Cursor = System.Windows.Forms.Cursors.IBeam; //还原鼠标形状
}
}
}
结果如下:大概有1/3的量。
时间:6个月
2014.12.22
1.之前只能统计一层文件夹,现在能计算多层文件夹
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;
namespace TextLineCounter
{
public partial class mainFrm : Form
{
public mainFrm()
{
InitializeComponent();
}
//存放文件
List<string> fileList = new List<string>();
private void btnCheck_Click(object sender, EventArgs e)
{
string folderPath = this.textBoxPath.Text;
if (folderPath == "")
{
FolderBrowserDialog folderDlg = new FolderBrowserDialog();
folderDlg.ShowDialog();
folderPath = folderDlg.SelectedPath;
this.textBoxPath.Text = folderPath;
}
if (folderPath != "")
{
SearchFolders(folderPath);
int[] LineArray = new int[fileList.Count];
for (int i = 0; i < fileList.Count; i++)
{
if (File.Exists(fileList[i]) && Path.GetFileName(fileList[i]) != "AssemblyInfo.cs")
{
//MessageBox.Show(fileList[i].ToString());
LineArray[i] = CountLines(fileList[i]);
}
}
int sum = 0;
for (int j = 0; j < LineArray.Length; j++)
{
sum += LineArray[j];
//MessageBox.Show(LineArray[j].ToString());
}
if (MessageBox.Show(sum.ToString()) == DialogResult.OK)
{
this.textBoxPath.Text = "";
fileList.Clear();
}
}
}
/// <summary>
/// 根据文件路径统计文件的行数
/// </summary>
/// <param name="path">路径</param>
/// <returns></returns>
public static int CountLines(string path)
{
//string path = @"c:\temp\MyTest.txt";
int lineCount = 0;
try
{
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
sr.ReadLine();
lineCount++;
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return lineCount;
}
/// <summary>
/// 找到路径下的文件夹
/// </summary>
/// <param name="searchDir">目录路径</param>
public void SearchFolders(string searchDir)
{
SearchFiles(searchDir);
string[] dirs;
try
{
dirs = Directory.GetDirectories(searchDir);
}
catch
{
dirs = null;
}
try
{
if (dirs != null)
{
foreach (string d in dirs)
{
this.SearchFolders(d);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 根据文件夹路径寻找文件
/// </summary>
/// <param name="folderPath">文件夹路径</param>
public void SearchFiles(string folderPath)
{
//目录信息
DirectoryInfo theFolder = new DirectoryInfo(folderPath);
//文件
foreach (FileInfo NextFile in theFolder.GetFiles())
{
string fileName = NextFile.Name.ToString();
int csLocate = fileName.LastIndexOf(".cs");
int designLocate = fileName.LastIndexOf(".D");
int cspLocate = fileName.LastIndexOf(".csp");
//MessageBox.Show(designLocate + "-" + csLocate.ToString() + "/" + fileName);
if (designLocate < 0 && cspLocate < 0 && csLocate > 0)
{
fileList.Add(theFolder + @"\" + fileName);
//MessageBox.Show(theFolder + "-" + fileName);
}
}
}
private void textBoxPath_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
this.textBoxPath.Cursor = System.Windows.Forms.Cursors.Arrow;//指定鼠标形状(更好看)
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void textBoxPath_DragDrop(object sender, DragEventArgs e)
{
//DataFormats 数据的格式,下有多个静态属性都为string型,除FileDrop格式外还有Bitmap,Text,WaveAudio等格式
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
this.textBoxPath.Text = path;
this.textBoxPath.Cursor = System.Windows.Forms.Cursors.IBeam; //还原鼠标形状
}
}
}
2014.12.27
1.使用EndsWith方法判断文件后缀,不用Path.GetExtension,是因为不能判断设计文件,.designer.cs
if (fileName.ToLower().EndsWith(".cs") && !fileName.ToLower().EndsWith(".designer.cs"))
{
fileList.Add(theFolder + @"\" + fileName);
}