C#计算代码行数

class Program
    {
        static void Main(string[] args)
        {
            int totalLineCount = 0;
            string directory;

            if(args.Length>0)
            {
                directory = args[0];
            }
            else
            {
                directory = Directory.GetCurrentDirectory();
            }

            directory = @"F:\workdocs";
            Console.WriteLine(directory);
            totalLineCount = DirectoryCountLines(directory);
            System.Console.WriteLine(totalLineCount);
            Console.ReadLine();
        }

        static int DirectoryCountLines(string directory)
        {
            int lineCount = 0;
            foreach(string file in Directory.GetFiles(directory,"*.cs"))
            {
                lineCount += CountLines(file);
            }

            foreach(string subdirectory in Directory.GetDirectories(directory))
            {
                lineCount += DirectoryCountLines(subdirectory);
            }
            return lineCount;
        }

        private static int CountLines(string file)
        {
            string line;
            int lineCount = 0;

            FileStream stream = new FileStream(file,FileMode.Open);
            StreamReader reader = new StreamReader(stream);

            line = reader.ReadLine();
            while(line!=null)
            {
                if(line.Trim()!="")
                {
                    lineCount++;
                }

                line = reader.ReadLine();
            }
            reader.Close();
            return lineCount;
        }
    }

 

posted @ 2015-12-14 21:30  Thinking.N  阅读(2004)  评论(0编辑  收藏  举报