1
using System;
2
using System.IO;
3
4
namespace CodeTell{
5
/// <summary>
6
/// CodeTell 的摘要说明。
7
/// </summary>
8
9
class CodeTell
10
{
11
private int codeLines = 0, commentLines = 0, fileCount = 0, floderCount = 0,characterCount = 0;
12
private long totalFileLength = 0;
13
/// <summary>
14
/// 应用程序的主入口点。
15
/// </summary>
16
[STAThread]
17
static void Main(string[] args)
18
{
19
//
20
// TODO: 在此处添加代码以启动应用程序
21
//
22
(new CodeTell()).Start();
23
24
}
25
26
public void Start()
27
{
28
DirectoryInfo folder = new DirectoryInfo(".");
29
30
examineList(folder);
31
32
//输出信息
33
Console.WriteLine("Folder:\t\t{0}",floderCount);
34
Console.WriteLine("Files:\t\t{0}",fileCount);
35
Console.WriteLine("Comment Lines:\t{0}",commentLines);
36
Console.WriteLine("Code Lines:\t{0}",codeLines);
37
Console.WriteLine("Char Count:\t{0}",characterCount);
38
Console.WriteLine("Files Length:\t{0}",totalFileLength);
39
40
Console.Read();
41
}
42
43
protected void examineList(DirectoryInfo dir)
44
{
45
FileInfo[] finfo = dir.GetFiles();
46
DirectoryInfo[] dinfo = dir.GetDirectories();
47
48
floderCount += dinfo.Length;
49
50
for(int i = 0; i<finfo.Length; i++)
51
{
52
calcFile(finfo[i]);
53
}
54
//遍历目录
55
for(int i = 0; i< dinfo.Length; i++)
56
{
57
examineList(dinfo[i]);
58
}
59
}
60
61
62
private void calcFile(FileInfo file)
63
{
64
StreamReader sreader;
65
string fname,line;
66
bool insideComment;
67
68
fname = file.FullName;
69
70
if(fname == null)
71
{
72
Console.WriteLine("Somehow the file was null");
73
}
74
//对.cs文件进行分析
75
else if(fname.ToLower().EndsWith(".cs"))
76
{
77
totalFileLength += file.Length;
78
fileCount++;
79
80
try
81
{
82
sreader = new StreamReader(fname);
83
Console.WriteLine(" {0}",fname);
84
line = sreader.ReadLine();
85
if( line != null)
86
line = line.Trim();
87
insideComment = false;
88
int temp;
89
while(line != null)
90
{
91
characterCount += line.Length;
92
if(line.Length == 0)
93
{
94
if((temp = line.IndexOf("*/")) >= 0)
95
{
96
insideComment = false;
97
if(line.Length > temp)
98
codeLines++;
99
if(temp > 2)
100
commentLines++;
101
}
102
}
103
else
104
{
105
if(!line.StartsWith("//"))
106
codeLines++;
107
if(line.StartsWith("/*"))
108
insideComment = true;
109
if(line.IndexOf("//")>=0)
110
commentLines++;
111
}
112
113
line = sreader.ReadLine();
114
if(line != null)
115
line = line.Trim();
116
117
}//end while
118
}
119
catch(Exception e)
120
{
121
Console.WriteLine(e.Message);
122
}
123
}
124
}
125
126
}
127
}
128

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128
