一个用来统计asp.net项目代码行数的小工具,很简单,应该不用解释了。
这东西以后会做在我的VS插件上面,现在暂时做成单独的小程序了,比较粗糙,将就着用吧,以后会升级的!
主要代码:
1 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Threading; namespace form { public partial class Form1 : Form { int files_num = 0; int all_lines = 0; int rem_lines = 0; int space_lines = 0; int except_file_num = 0; string file_ext = ".txt|.cs|.aspx|.html|.htm"; Thread work; public Form1() { InitializeComponent(); //Control.CheckForIllegalCrossThreadCalls = false; } private void button2_Click(object sender, EventArgs e) { DialogResult result=this.folderBrowserDialog1.ShowDialog(); this.textBox3.Text = this.folderBrowserDialog1.SelectedPath; } private void button1_Click(object sender, EventArgs e) { files_num = 0; all_lines = 0; rem_lines = 0; space_lines = 0; except_file_num = 0; this.progressBar1.Value = 0; if (this.textBox3.Text == "") { MessageBox.Show("请先指定项目目录!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } work = new Thread(new ThreadStart(StartWork)); work.Start(); } private void StartWork() { MethodInvoker mi = new MethodInvoker(Thread_Proc); BeginInvoke(mi); } private void Thread_Proc() { //统计文件数 CountFiles(this.textBox3.Text); this.progressBar1.Minimum = 0; this.progressBar1.Maximum = files_num; CountLines(this.textBox3.Text); } private void CountFiles(string folderPath) { DirectoryInfo selected_dir = new DirectoryInfo(folderPath); if (!selected_dir.Exists) return; FileInfo[] files = selected_dir.GetFiles(); foreach (var item in files) { files_num++; } foreach (var item in selected_dir.GetDirectories()) { CountFiles(folderPath + "\\" + item.Name); } } private void CountLines(string folderPath) { DirectoryInfo selected_dir = new DirectoryInfo(folderPath); if (!selected_dir.Exists) return; FileInfo[] files = selected_dir.GetFiles(); foreach(var file in files) { try { SetLabelValue3(folderPath+"\\"+file.Name); //判断扩展名 string[] ext=file_ext.Split('|'); string cur_ext=file.Extension; bool find=false; foreach(var item in ext) { if (cur_ext == item) { find = true; break; } } if(!find) { except_file_num++; this.progressBar1.Value++; Thread.Sleep(100); continue; } string[] lines = File.ReadAllLines(folderPath+"\\"+file.Name); CountRems(lines); foreach (var item in lines) { string tmp_item = item.Trim(); if (tmp_item == "") { space_lines++; SetLabelValue4(space_lines.ToString()); } all_lines++; SetLabelValue1(all_lines.ToString()); } } catch (Exception ex) { //以防权限等错误,本版本忽略掉不能按行读取的文件 MessageBox.Show(ex.Message); } this.progressBar1.Value++; Thread.Sleep(100); } foreach (var item in selected_dir.GetDirectories()) { CountLines(folderPath+"\\"+item.Name); } SetLabelValue3("完成!存在" + except_file_num.ToString() + "个非源码文件!"); } void CountRems(string[] lines) { //寻找 "//" 注释行 foreach (var item in lines) { string tmp_item = item.Trim(); if (tmp_item.Length > 2 && tmp_item.Substring(0, 2) == "//") { rem_lines++; SetLabelValue2(rem_lines.ToString()); } } int rem_span = 0; bool findhead=false; //寻找 "/*……*/" 注释块 foreach (var item in lines) { string tmp_item = item.Trim(); if (tmp_item.Length > 2 && tmp_item.Substring(0, 2) == "/*") { findhead = true; } if (tmp_item.Length > 2 && tmp_item.Substring(0, 2) == "*/") { findhead = false; } if (findhead) { rem_span++; rem_lines++; } } } delegate void SetLabelCallback(Label lb,string text); delegate void SetTextCallback(string text); void SetLabelValue(Label lb, string text) { if (lb.InvokeRequired) { SetLabelCallback d = new SetLabelCallback(SetLabelValue); lb.Invoke(d, new object[] { text }); } else { lb.Text = text; } lb.Refresh(); Application.DoEvents(); } void SetLabelValue1(string text) { if (this.label5.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetLabelValue1); this.label5.Invoke(d, new object[] { text }); } else { this.label5.Text = text; } this.label5.Refresh(); Application.DoEvents(); } void SetLabelValue2(string text) { if (this.label6.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetLabelValue2); this.label6.Invoke(d, new object[] { text }); } else { this.label6.Text = text; } this.label6.Refresh(); Application.DoEvents(); } void SetLabelValue3(string text) { if (this.label7.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetLabelValue3); this.label7.Invoke(d, new object[] { text }); } else { this.label7.Text = text; } this.label7.Refresh(); Application.DoEvents(); } void SetLabelValue4(string text) { if (this.label9.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetLabelValue4); this.label9.Invoke(d, new object[] { text }); } else { this.label9.Text = text; } this.label9.Refresh(); Application.DoEvents(); } void DelegateSetValue(string text) { this.label5.Text = text; this.label6.Text = text; this.label7.Text = text; this.label9.Text = text; } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { this.linkLabel1.Links[0].LinkData = "http://www.cnzui.com"; System.Diagnostics.Process.Start(e.Link.LinkData.ToString()); } } } |
郑重声明:
除特别声明为转载内容外,本站所有内容均为作者原创,谢绝任何单位和个人不经许可的复制和转播!
对于确有转载需要的,请先与作者联系,在获得允许后烦请在转载时保留文章出处。
本文出自Lupin's Blog:http://www.cnzui.com/archives/888
除特别声明为转载内容外,本站所有内容均为作者原创,谢绝任何单位和个人不经许可的复制和转播!
对于确有转载需要的,请先与作者联系,在获得允许后烦请在转载时保留文章出处。
本文出自Lupin's Blog:http://www.cnzui.com/archives/888