白开心

  IT博客 :: 首页 ::  :: 联系 :: 聚合  :: 管理 ::
  9 随笔 :: 76 文章 :: 28 评论 :: 0 Trackbacks

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CW
{
    public class CodeLineCount
    {
        /// <summary>
        /// 计算文件的代码行数
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static int ReadFileLinesCount(String filePath)
        {
            if (!File.Exists(filePath)) return 0;

            FileStream fs = null;
            StreamReader sr = null;
            String LineContent = String.Empty;
            int lineCount = 0;
            bool more = false;
            String[] StartString = new String[]{"//","{","}","#region","#endregion","using","namespace"};  ///不记录代码行数的起始字符串

            try
            {
                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                sr = new StreamReader(fs);
                while (!sr.EndOfStream)
                {
                    LineContent = sr.ReadLine().Trim();
                    ///如果是空行,不记录
                    if (LineContent == String.Empty)
                    {
                        continue;
                    }

                    ///首先判断行开头是否包含起始字符串
                    foreach (String s in StartString)
                    {
                        if (LineContent.StartsWith(s))
                        {
                            goto NEXT;
                        }
                    }

                    ///记录该行有多行注释
                    if (LineContent.IndexOf("/*") > -1)
                    {
                        if (LineContent.LastIndexOf("*/") > LineContent.LastIndexOf("/*"))
                        {
                            lineCount++;
                        }
                        else
                        {
                            more = true;
                        }
                        continue;
                    }

                    ///如果是多行注释开头的
                    if (more)
                    {
                        if (LineContent.IndexOf("*/") > -1 && LineContent.LastIndexOf("/*") < LineContent.LastIndexOf("*/"))
                        {
                            more = false;
                            ///计算 /**//**/ 之间是否有代码,该处应该用循环,未写上
                            if (!LineContent.EndsWith("*/"))
                            {
                                lineCount++;
                            }
                        }
                        continue;
                    }
                    else
                    {
                        lineCount++;
                    }

                NEXT: ;
                }
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                    sr = null;
                }
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
            }
            return lineCount;
        }

        public static int ReadFolderCount(String folderPath)
        {
            int lineCount = 0;
            foreach (String file in Directory.GetFiles(folderPath,"*.cs"))
            {
                lineCount += ReadFileLinesCount(file);
            }

            foreach (String folder in Directory.GetDirectories( folderPath))
            {
                lineCount += ReadFolderCount(folder);
            }
            return lineCount;
        }
    }
}

posted on 2007-11-05 17:25 白开心 阅读(153) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。