C# Code Summary

nonamedemo發表於2007-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
{
///


/// 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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章