代码统计工具(支持C#,VB.NET)
2008-03-31 11:26
357 查看
小玩意,不多说,想看的一看就会,
界面如下:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/lynnlin1122/a68b264eb09770dad0c86a7e.jpg)
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 代码行数统计工具
{
public partial class frmCountCodes : Form
{
string curDir="";//当前目录
string wrfile = "";//代码写入的文件
string filter = "";//要查找的文件类型
bool chtxt = false;//是否手动更改目录
//private delegate void desearch(string str);//查找委托
long count = 0;//行数
int filecount = 0;//发现文件个数
StringBuilder strinfo;//返回信息
bool ischeck = false;//是否为连续多行的注释 如C#中的 /*...*/
public frmCountCodes()
{
InitializeComponent();
}
/// <summary>
/// 设置目录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDir_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "选择要统计的文件所在目录";
fbd.RootFolder =Environment.SpecialFolder.MyComputer;
fbd.ShowNewFolderButton = false;
fbd.ShowDialog();
if (fbd.SelectedPath.Trim() != "")
{
curDir = fbd.SelectedPath;
txtDir.Text = curDir;
}
}
private void txtDir_TextChanged(object sender, EventArgs e)
{
chtxt = true;
}
private void txtDir_Leave(object sender, EventArgs e)
{
if (chtxt)
{
if (Directory.Exists(txtDir.Text.Trim()))
{
curDir = txtDir.Text.Trim();
}
else
{
MessageBox.Show("此并不是一个有效的目录!请重新设置。");
txtDir.Text = curDir;
}
}
}
//统计
private void btnser_Click(object sender, EventArgs e)
{
try
{
btnser.Enabled = !btnser.Enabled;
if (!Directory.Exists(curDir))
{
MessageBox.Show("此并不是一个有效的目录!请重新设置。");
btnser.Enabled = !btnser.Enabled;
return;
}
filter = "";
//desearch searchfile = new desearch(search);
count = 0;
filecount = 0;
strinfo = new StringBuilder();
if (chvb.Checked)//选择了VB
{
filter += "*.vb,";
//IAsyncResult ir=searchfile.BeginInvoke("*.vb", null, null);
//while (!ir.IsCompleted)//异步执行
//{
// Application.DoEvents();
//}
//searchfile.EndInvoke(ir);
search("*.vb");
}
if (chCSharp.Checked)//选择了C#
{
filter += "*.cs,";
//IAsyncResult ir = searchfile.BeginInvoke("*.cs", null, null);
//while (!ir.IsCompleted)//异步执行
//{
// Application.DoEvents();
//}
//searchfile.EndInvoke(ir);
search("*.cs");
}
if (filter.Trim() == "")
{
MessageBox.Show("请选择要统计的语言种类");
btnser.Enabled = !btnser.Enabled;
return;
}
strinfo.AppendLine("主目录:" + curDir);
strinfo.AppendLine("所选择的文件类型有:" + filter.Substring(0, filter.Length - 1));
strinfo.AppendLine("发现文件个数为:" + filecount);
strinfo.AppendLine("代码总行数为:" + count);
txtinfo.Text = strinfo.ToString();
}
catch
{
MessageBox.Show("未知错误,请确保路径正确和其子目录可读!", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
btnser.Enabled = !btnser.Enabled;
}
private void search(string ftype)
{
string[] files;
if (chChirld.Checked)
{
files = Directory.GetFiles(curDir, ftype, SearchOption.AllDirectories);//遍历所有子目录
}
else
{
files = Directory.GetFiles(curDir, ftype, SearchOption.TopDirectoryOnly);
}
StreamWriter sw =null;
if (wrfile != "")
{
try
{
if (!File.Exists(wrfile))
{
FileStream f = File.Create(wrfile);
f.Close();
}
sw = new StreamWriter(wrfile,false,Encoding.GetEncoding("gb2312"));
}
catch
{
sw = null;
}
}
foreach (string file in files)
{
if (file.EndsWith(ftype.Replace("*", "Designer"))) continue;//资源文件
filecount ++;
StreamReader sr = new StreamReader(file,Encoding.GetEncoding("gb2312"));
lbSearchFile.Text = "当前统计文件:" + Path.GetFileName(file);
Application.DoEvents();
string line;//当前读取的行
while (!sr.EndOfStream)
{
line = sr.ReadLine().Trim();
if (sw!=null&&line != "") sw.WriteLine(line);
if (!checktype(line))
{
continue;//如果不是有效代码
}
count++;
}
sr.Close();
}
if (sw != null) {sw.Close();}
//sw.Close();
lbSearchFile.Text = "";
}
/// <summary>
/// 判断是否为有效代码行
/// </summary>
/// <param name="strline">代码行</param>
/// <returns></returns>
private bool checktype(string strline)
{
bool isok = true;
//C#代码判断
if (!ischeck && strline.StartsWith("/*") && strline.IndexOf("*/")<0)
{
isok = false;
ischeck = true;//为连续注释
}
else
{
if (ischeck&&strline.IndexOf("*/") >= 0)
{
ischeck = false;
}
}
if (ischeck||strline.StartsWith("//") || (strline.IndexOf("/*") <= 0 && strline.EndsWith("*/")) || strline == "{" || strline == "}")
{
isok = false;
}
else
if (strline.StartsWith("'")||strline=="")//vb.net代码
isok = false;
return isok;
}
private void 统计FToolStripMenuItem_Click(object sender, EventArgs e)
{
if(btnser.Enabled) btnser_Click(sender, e);
}
private void 设置目录CToolStripMenuItem_Click(object sender, EventArgs e)
{
btnDir_Click(sender,e);
}
private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnser_EnabledChanged(object sender, EventArgs e)
{
统计FToolStripMenuItem.Enabled = btnser.Enabled;
}
//选择保存路径
private void btnwr_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.AddExtension = true;
//sfd.CheckFileExists = true;
sfd.DefaultExt = "*.doc";
sfd.Filter = "Word File|*.doc;*.wps|TXT FILE|*.txt";
sfd.ShowDialog();
if (sfd.FileName.Trim() != "") { wrfile = sfd.FileName; txtWrite.Text = wrfile; }
}
private void txtWrite_TextChanged(object sender, EventArgs e)
{
wrfile = txtWrite.Text;
}
}
}
界面如下:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/lynnlin1122/a68b264eb09770dad0c86a7e.jpg)
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 代码行数统计工具
{
public partial class frmCountCodes : Form
{
string curDir="";//当前目录
string wrfile = "";//代码写入的文件
string filter = "";//要查找的文件类型
bool chtxt = false;//是否手动更改目录
//private delegate void desearch(string str);//查找委托
long count = 0;//行数
int filecount = 0;//发现文件个数
StringBuilder strinfo;//返回信息
bool ischeck = false;//是否为连续多行的注释 如C#中的 /*...*/
public frmCountCodes()
{
InitializeComponent();
}
/// <summary>
/// 设置目录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDir_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "选择要统计的文件所在目录";
fbd.RootFolder =Environment.SpecialFolder.MyComputer;
fbd.ShowNewFolderButton = false;
fbd.ShowDialog();
if (fbd.SelectedPath.Trim() != "")
{
curDir = fbd.SelectedPath;
txtDir.Text = curDir;
}
}
private void txtDir_TextChanged(object sender, EventArgs e)
{
chtxt = true;
}
private void txtDir_Leave(object sender, EventArgs e)
{
if (chtxt)
{
if (Directory.Exists(txtDir.Text.Trim()))
{
curDir = txtDir.Text.Trim();
}
else
{
MessageBox.Show("此并不是一个有效的目录!请重新设置。");
txtDir.Text = curDir;
}
}
}
//统计
private void btnser_Click(object sender, EventArgs e)
{
try
{
btnser.Enabled = !btnser.Enabled;
if (!Directory.Exists(curDir))
{
MessageBox.Show("此并不是一个有效的目录!请重新设置。");
btnser.Enabled = !btnser.Enabled;
return;
}
filter = "";
//desearch searchfile = new desearch(search);
count = 0;
filecount = 0;
strinfo = new StringBuilder();
if (chvb.Checked)//选择了VB
{
filter += "*.vb,";
//IAsyncResult ir=searchfile.BeginInvoke("*.vb", null, null);
//while (!ir.IsCompleted)//异步执行
//{
// Application.DoEvents();
//}
//searchfile.EndInvoke(ir);
search("*.vb");
}
if (chCSharp.Checked)//选择了C#
{
filter += "*.cs,";
//IAsyncResult ir = searchfile.BeginInvoke("*.cs", null, null);
//while (!ir.IsCompleted)//异步执行
//{
// Application.DoEvents();
//}
//searchfile.EndInvoke(ir);
search("*.cs");
}
if (filter.Trim() == "")
{
MessageBox.Show("请选择要统计的语言种类");
btnser.Enabled = !btnser.Enabled;
return;
}
strinfo.AppendLine("主目录:" + curDir);
strinfo.AppendLine("所选择的文件类型有:" + filter.Substring(0, filter.Length - 1));
strinfo.AppendLine("发现文件个数为:" + filecount);
strinfo.AppendLine("代码总行数为:" + count);
txtinfo.Text = strinfo.ToString();
}
catch
{
MessageBox.Show("未知错误,请确保路径正确和其子目录可读!", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
btnser.Enabled = !btnser.Enabled;
}
private void search(string ftype)
{
string[] files;
if (chChirld.Checked)
{
files = Directory.GetFiles(curDir, ftype, SearchOption.AllDirectories);//遍历所有子目录
}
else
{
files = Directory.GetFiles(curDir, ftype, SearchOption.TopDirectoryOnly);
}
StreamWriter sw =null;
if (wrfile != "")
{
try
{
if (!File.Exists(wrfile))
{
FileStream f = File.Create(wrfile);
f.Close();
}
sw = new StreamWriter(wrfile,false,Encoding.GetEncoding("gb2312"));
}
catch
{
sw = null;
}
}
foreach (string file in files)
{
if (file.EndsWith(ftype.Replace("*", "Designer"))) continue;//资源文件
filecount ++;
StreamReader sr = new StreamReader(file,Encoding.GetEncoding("gb2312"));
lbSearchFile.Text = "当前统计文件:" + Path.GetFileName(file);
Application.DoEvents();
string line;//当前读取的行
while (!sr.EndOfStream)
{
line = sr.ReadLine().Trim();
if (sw!=null&&line != "") sw.WriteLine(line);
if (!checktype(line))
{
continue;//如果不是有效代码
}
count++;
}
sr.Close();
}
if (sw != null) {sw.Close();}
//sw.Close();
lbSearchFile.Text = "";
}
/// <summary>
/// 判断是否为有效代码行
/// </summary>
/// <param name="strline">代码行</param>
/// <returns></returns>
private bool checktype(string strline)
{
bool isok = true;
//C#代码判断
if (!ischeck && strline.StartsWith("/*") && strline.IndexOf("*/")<0)
{
isok = false;
ischeck = true;//为连续注释
}
else
{
if (ischeck&&strline.IndexOf("*/") >= 0)
{
ischeck = false;
}
}
if (ischeck||strline.StartsWith("//") || (strline.IndexOf("/*") <= 0 && strline.EndsWith("*/")) || strline == "{" || strline == "}")
{
isok = false;
}
else
if (strline.StartsWith("'")||strline=="")//vb.net代码
isok = false;
return isok;
}
private void 统计FToolStripMenuItem_Click(object sender, EventArgs e)
{
if(btnser.Enabled) btnser_Click(sender, e);
}
private void 设置目录CToolStripMenuItem_Click(object sender, EventArgs e)
{
btnDir_Click(sender,e);
}
private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnser_EnabledChanged(object sender, EventArgs e)
{
统计FToolStripMenuItem.Enabled = btnser.Enabled;
}
//选择保存路径
private void btnwr_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.AddExtension = true;
//sfd.CheckFileExists = true;
sfd.DefaultExt = "*.doc";
sfd.Filter = "Word File|*.doc;*.wps|TXT FILE|*.txt";
sfd.ShowDialog();
if (sfd.FileName.Trim() != "") { wrfile = sfd.FileName; txtWrite.Text = wrfile; }
}
private void txtWrite_TextChanged(object sender, EventArgs e)
{
wrfile = txtWrite.Text;
}
}
}
相关文章推荐
- 代码统计工具(支持C#,VB.NET)
- 代码统计工具(支持C#,VB.NET)
- C# VB.Net代码互转 使用Rosyln 支持本地转换 支持匿名表达式等等
- 发布NBearLite v1.0.0 beta - 全面支持SqlServer,Oracle,MySql,PostgreSql数据库存储过程调用代码生成(C#/VB.NET)
- 发布NBearLite v1.0.0 beta - 全面支持SqlServer,Oracle,MySql,PostgreSql数据库存储过程调用代码生成(C#/VB.NET)
- 一个简单的动态编译器,支持C#和VB.NET。当你想测试一些简单的代码的时候可以使用。
- 发布NBearLite v1.0.0 beta - 全面支持SqlServer,Oracle,MySql,PostgreSql数据库存储过程调用代码生成(C#/VB.NET)
- C#.NET中类似vb,asp中ASC和CHR的函数
- C# 默认命名空间 与 VB.net 根命名空间
- [轉]vb.net与c#相互转换工具(downmoon)
- 16进制字符串转数字(C/C++,VB/VB.net,C#)
- 使用Web服务将C#代码转换为VB.NET代码
- vb.net与c#相互转换工具
- 一个DataGrid分页控件,c#写的,再vb.net中调用,将会不断完善的
- VB.net与C#的异同
- C#、VB.NET_关于图片的缩放
- (.NET) IntelliSense difference of Extension Method name in Statement Completion for VB and C#.
- 在.net(C# or vb.net)中如何取消一个窗体的关闭
- 日本财政年度取得(c#,vb.net)
- C# 中对字符串是否为数字的判断(类似 vb.net 中的 IsNumber)