C# Code Summary

11月16日(星期五)

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace prjCodeCount
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtPath;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox chkFolder;
private System.Windows.Forms.TextBox txtResult;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public frmMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtPath = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.chkFolder = new System.Windows.Forms.CheckBox();
this.txtResult = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtPath
//
this.txtPath.Location = new System.Drawing.Point(16, 16);
this.txtPath.Name = "txtPath";
this.txtPath.ReadOnly = true;
this.txtPath.Size = new System.Drawing.Size(320, 21);
this.txtPath.TabIndex = 0;
this.txtPath.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(336, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "浏览(&B)...";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// chkFolder
//
this.chkFolder.Location = new System.Drawing.Point(416, 16);
this.chkFolder.Name = "chkFolder";
this.chkFolder.Size = new System.Drawing.Size(64, 24);
this.chkFolder.TabIndex = 2;
this.chkFolder.Text = "文件夹";
//
// txtResult
//
this.txtResult.Location = new System.Drawing.Point(16, 48);
this.txtResult.Multiline = true;
this.txtResult.Name = "txtResult";
this.txtResult.Size = new System.Drawing.Size(456, 368);
this.txtResult.TabIndex = 3;
this.txtResult.Text = "";
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(488, 429);
this.Controls.Add(this.txtResult);
this.Controls.Add(this.chkFolder);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtPath);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frmMain";
this.Text = "代码统计";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}

private void button1_Click(object sender, System.EventArgs e)
{
al=new ArrayList();
if(this.chkFolder.Checked==true)
{
CountFolder();
}
else
{
CountFile();
}
}
private void CountFolder()
{
FolderBrowserDialog fbd=new FolderBrowserDialog();
DialogResult dr=fbd.ShowDialog();
if(dr==DialogResult.Cancel)
{
return;
}
this.txtPath.Text=fbd.SelectedPath;
ClearItem();
CountAllFile(new DirectoryInfo(this.txtPath.Text));
this.txtResult.Text+=GetTotalInfo();
}
ArrayList al;
private void CountAllFile(DirectoryInfo di)
{
FileInfo[] fis=di.GetFiles("*.cs");
foreach(FileInfo temp in fis)
{
Result r=new Result(temp);
al.Add(r);
this.txtResult.Text+=r.ToString();
}
DirectoryInfo[] dis=di.GetDirectories();
foreach(DirectoryInfo temp in dis)
{
CountAllFile(temp);
}
}
private string GetTotalInfo()
{

int blank=0;
int comment=0;
int code=0;
foreach(object obj in al)
{
Result r=(Result)obj;
blank+=r.Blank;
comment+=r.Comment;
code+=r.Code;
}
int sum=blank+comment+code;
float blk=(float)blank/sum*100;
float cmt=(float)comment/sum*100;
string s="总计";
s+="t";
s+=blank.ToString()+"t";
s+=comment.ToString()+"t";
s+=code.ToString()+"t";
s+=blk.ToString("0.00")+"%t";
s+=cmt.ToString("0.00")+"%rn";
return s;

}
private void CountFile()
{
OpenFileDialog fbd=new OpenFileDialog();
DialogResult dr=fbd.ShowDialog();
if(dr==DialogResult.Cancel)
{
return;
}
this.txtPath.Text=fbd.FileName;
Result r=new Result(new FileInfo(this.txtPath.Text));
al.Add(r);
ClearItem();
this.txtResult.Text+=r.ToString();
this.txtResult.Text+=GetTotalInfo();
}
private void ClearItem()
{
this.txtResult.Text="文件名t空行t注释行t代码行t空行率t注释率rn";
}
}

 

 

public class Result
{
int blank;
int comment;
int code;
public int Blank
{
get{return this.blank;}
}
public int Comment
{
get{return this.comment;}
}
public int Code
{
get{return this.code;}
}
FileInfo fi;

public Result(FileInfo fi)
{
this.fi=fi;
FileStream fs=new FileStream(fi.FullName,FileMode.Open,FileAccess.Read);
StreamReader sr=new StreamReader(fs,System.Text.Encoding.GetEncoding("gb2312"));
while(true)
{
string s=sr.ReadLine();
if(s==null)
{
break;
}
s=s.Trim().Replace("t","");
if(s=="")
{
blank++;
}
else if(s.StartsWith(@"//"))
{
comment++;
}
else
{
code++;
}
}
}
public override string ToString()
{
int sum=blank+comment+code;
float blk=(float)blank/sum*100;
float cmt=(float)comment/sum*100;
string s="";
s+=fi.Name+"t";
s+=blank.ToString()+"t";
s+=comment.ToString()+"t";
s+=code.ToString()+"t";
s+=blk.ToString("0.00")+"%t";
s+=cmt.ToString("0.00")+"%rn";
return s;
}


}

 


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值