代码统计工具(支持C#,VB.NET)

小玩意,不多说,想看的一看就会,

界面如下:

 

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;
        }
    }
}
align="center" marginwidth="0" marginheight="0" src="http://www.blogbao.com/script.aspx?userid=21504&AdType=0&AdstyleID=49640&Direction=1" frameborder="0" width="490" scrolling="no" height="150">
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值