C# Code Summary
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;
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10395457/viewspace-982869/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- LeetCode-Summary RangesLeetCode
- Xcode變數概覽-summaryXCode變數
- 【LeetCode 228_陣列】Summary RangesLeetCode陣列
- LeetCode Monotone Stack Summary 單調棧小結LeetCodeMono
- ACM summaryACM
- Mongoose SummaryGo
- System design summary
- LeetCode Binary Search Summary 二分搜尋法小結LeetCode
- Summary Note Index for BasicFiles and SecureFilesIndex
- Programming languages Domain summaryAI
- c#練手codeC#
- Microsoft Windows Bitmap File Format SummaryROSWindowsORM
- GUID UUID in Java SummaryGUIJava
- The Tokenizers Summary: [EOS],[BOS],[CLS],[SEP]
- C# Unicode編碼C#Unicode
- 【文件學習】tensorboardX——summary writerORB
- Microsoft Application Architecture Solution SummaryROSAPP
- Asp.Net Control SummaryASP.NET
- Summary Functions and Maps(pandas學習三)Function
- TreeView.cs source code in C# .NETViewC#
- An Algorithm Summary of Programming Collective Intelligence (4)GoIntel
- mysql 動態引數(Dynamic System Variable Summary)MySql
- Summary01 - cron任務、grep、find命令
- 2024 Mar. Week-3 Summary
- asp.net(C#) 編碼解碼(HtmlEncode與HtmlEncode)ASP.NETC#HTML
- codesoft在delphi,C#中的例子C#
- Summary For Forcing The Database Open With `_ALLOW_RESETLOGS_CORRUPTION`Database
- percona-toolkit 之 【pt-summary】、【pt-mysql-summary】、【pt-config-diff】、【pt-variable-advisor】說明MySql
- 三個不常用的HTML元素:<details>、<summary>、<dialog>HTMLAI
- MySQL執行狀態監控(pt-mysql-summary)MySql
- Oracle學習系列—資料庫優化—Statistics SummaryOracle資料庫優化
- 【28】VsCode如何執行C#程式碼VSCodeC#
- C#移除字串中的不可見Unicode字元C#字串Unicode字元
- C# zip/unzip with ICSharpCode.SharpZipLibC#CSharpRPC
- Percona-Tookit工具包之pt-mysql-summaryMySql
- The 1st week summary of devtools community in learnku.comdevUnity
- C# 寫 LeetCode easy #14 Longest Common PrefixC#LeetCode
- C# 讀取 ttf字型檔案裡的 UnicodeC#Unicode