ITPub博客

首页 > 应用开发 > IT综合 > C# Code Summary

C# Code Summary

原创 IT综合 作者:nonamedemo 时间:2007-11-16 23:13:43 0 删除 编辑

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

namespace prjCodeCount
{
///


/// Form1 的摘要说明。
///

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;
///
/// 必需的设计器变量。
///

private System.ComponentModel.Container components = null;

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

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

///


/// 清理所有正在使用的资源。
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
///


/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

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

///


/// 应用程序的主入口点。
///

[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;
}


}


}

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10395457/viewspace-982869/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    31
  • 访问量
    11653