├── .gitignore ├── 012sys.ico ├── App.config ├── Config.cs ├── FileCounter.csproj ├── FileCounter.csproj.user ├── FileCounter.sln ├── FormMain.Designer.cs ├── FormMain.cs ├── FormMain.resx ├── Helper.cs ├── Images ├── console.png └── form.png ├── Logger.cs ├── Program.cs ├── Properties ├── AssemblyInfo.cs ├── Resources.Designer.cs └── Resources.resx ├── README.md ├── Scanner.cs └── bin └── Debug ├── FileCounter.exe.config ├── FileCounter.pdb ├── Test └── Config.cs ├── out.txt └── test.bat /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | /obj 30 | /.vs 31 | -------------------------------------------------------------------------------- /012sys.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/surfsky/FileCounter/077fc6aa2f1adcc4043fbe5aaeb99ea6173860f3/012sys.ico -------------------------------------------------------------------------------- /App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Config.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace FileCounter 8 | { 9 | /// 10 | /// 运行参数 11 | /// 12 | public class Config 13 | { 14 | // 源控制 15 | public string RootFolder { get; set; } 16 | public string[] Extensions { get; set; } 17 | public string Encoding { get; set; } 18 | public Encoding Enc 19 | { 20 | get 21 | { 22 | try { return System.Text.Encoding.GetEncoding(this.Encoding); } 23 | catch { return System.Text.Encoding.UTF8; } 24 | } 25 | } 26 | 27 | // 辅助参数 28 | public string[] SkipSubFolders { get; set; } 29 | public bool SkipBlankLines { get; set; } 30 | public bool SkipCommentLines { get; set; } 31 | public bool SkipHidden { get; set; } 32 | 33 | // 输出控制 34 | public string OutFile { get; set; } 35 | public int OutLines { get; set; } = 9000; 36 | 37 | // 其它 38 | public bool OpenWhenFinished { get; set; } 39 | 40 | 41 | /// 从 App.config 配置文件中读取 42 | public static Config ReadFromAppConfig() 43 | { 44 | var cfg = new Config(); 45 | cfg.Extensions = ConfigurationManager.AppSettings["Extensions"].ToArray(); 46 | cfg.SkipSubFolders = ConfigurationManager.AppSettings["SkipSubFolders"].ToArray(); 47 | cfg.SkipHidden = ConfigurationManager.AppSettings["SkipHidden"].ToBool(); 48 | cfg.SkipBlankLines = ConfigurationManager.AppSettings["SkipBlankLines"].ToBool(); 49 | cfg.SkipCommentLines = ConfigurationManager.AppSettings["SkipCommentLines"].ToBool(); 50 | cfg.OpenWhenFinished = ConfigurationManager.AppSettings["OpenWhenFinished"].ToBool(); 51 | cfg.Encoding = ConfigurationManager.AppSettings["Encoding"]; 52 | return cfg; 53 | } 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /FileCounter.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {4DEC4AEA-9D5D-4E29-A242-67403B901A83} 9 | Exe 10 | Properties 11 | FileCounter 12 | FileCounter 13 | v4.8 14 | 15 | 16 | 512 17 | 18 | 19 | x86 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | false 28 | 29 | 30 | x86 31 | pdbonly 32 | true 33 | bin\Release\ 34 | TRACE 35 | prompt 36 | 4 37 | false 38 | 39 | 40 | 41 | 42 | 43 | 012sys.ico 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | Form 61 | 62 | 63 | FormMain.cs 64 | 65 | 66 | 67 | 68 | 69 | 70 | True 71 | True 72 | Resources.resx 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | FormMain.cs 82 | 83 | 84 | ResXFileCodeGenerator 85 | Resources.Designer.cs 86 | 87 | 88 | 89 | 90 | 91 | 92 | 99 | -------------------------------------------------------------------------------- /FileCounter.csproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /FileCounter.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.8.34330.188 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileCounter", "FileCounter.csproj", "{4DEC4AEA-9D5D-4E29-A242-67403B901A83}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{EE68D564-3328-490E-AA34-750E5C435DA7}" 9 | ProjectSection(SolutionItems) = preProject 10 | Readme.md = Readme.md 11 | EndProjectSection 12 | EndProject 13 | Global 14 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 15 | Debug|x86 = Debug|x86 16 | Release|x86 = Release|x86 17 | EndGlobalSection 18 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 19 | {4DEC4AEA-9D5D-4E29-A242-67403B901A83}.Debug|x86.ActiveCfg = Debug|x86 20 | {4DEC4AEA-9D5D-4E29-A242-67403B901A83}.Debug|x86.Build.0 = Debug|x86 21 | {4DEC4AEA-9D5D-4E29-A242-67403B901A83}.Release|x86.ActiveCfg = Release|x86 22 | {4DEC4AEA-9D5D-4E29-A242-67403B901A83}.Release|x86.Build.0 = Release|x86 23 | EndGlobalSection 24 | GlobalSection(SolutionProperties) = preSolution 25 | HideSolutionNode = FALSE 26 | EndGlobalSection 27 | GlobalSection(ExtensibilityGlobals) = postSolution 28 | SolutionGuid = {C4BCD58E-93CD-4F8E-B21E-09B3740C9CAE} 29 | EndGlobalSection 30 | EndGlobal 31 | -------------------------------------------------------------------------------- /FormMain.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace FileCounter 2 | { 3 | partial class FormMain 4 | { 5 | /// 6 | /// Required designer variable. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// Clean up any resources being used. 12 | /// 13 | /// true if managed resources should be disposed; otherwise, false. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows Form Designer generated code 24 | 25 | /// 26 | /// Required method for Designer support - do not modify 27 | /// the contents of this method with the code editor. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormMain)); 32 | this.label1 = new System.Windows.Forms.Label(); 33 | this.tbFolder = new System.Windows.Forms.TextBox(); 34 | this.btnFolder = new System.Windows.Forms.Button(); 35 | this.label2 = new System.Windows.Forms.Label(); 36 | this.tbExtensions = new System.Windows.Forms.TextBox(); 37 | this.btnGo = new System.Windows.Forms.Button(); 38 | this.btnAbout = new System.Windows.Forms.Button(); 39 | this.chkSkipBlankLines = new System.Windows.Forms.CheckBox(); 40 | this.label3 = new System.Windows.Forms.Label(); 41 | this.chkSkipCommentLines = new System.Windows.Forms.CheckBox(); 42 | this.picBanner = new System.Windows.Forms.PictureBox(); 43 | this.lblApp = new System.Windows.Forms.Label(); 44 | this.label5 = new System.Windows.Forms.Label(); 45 | this.lblVersion = new System.Windows.Forms.Label(); 46 | this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); 47 | this.label7 = new System.Windows.Forms.Label(); 48 | this.tbLines = new System.Windows.Forms.NumericUpDown(); 49 | this.tbOutFile = new System.Windows.Forms.TextBox(); 50 | this.label4 = new System.Windows.Forms.Label(); 51 | this.chkOpenWhenFinished = new System.Windows.Forms.CheckBox(); 52 | this.btnFile = new System.Windows.Forms.Button(); 53 | this.tbEncoding = new System.Windows.Forms.TextBox(); 54 | this.label6 = new System.Windows.Forms.Label(); 55 | this.tbSkipSubFolders = new System.Windows.Forms.TextBox(); 56 | this.label8 = new System.Windows.Forms.Label(); 57 | this.chkSkipHidden = new System.Windows.Forms.CheckBox(); 58 | this.label9 = new System.Windows.Forms.Label(); 59 | this.label10 = new System.Windows.Forms.Label(); 60 | ((System.ComponentModel.ISupportInitialize)(this.picBanner)).BeginInit(); 61 | ((System.ComponentModel.ISupportInitialize)(this.tbLines)).BeginInit(); 62 | this.SuspendLayout(); 63 | // 64 | // label1 65 | // 66 | this.label1.AutoSize = true; 67 | this.label1.Location = new System.Drawing.Point(11, 119); 68 | this.label1.Name = "label1"; 69 | this.label1.Size = new System.Drawing.Size(41, 12); 70 | this.label1.TabIndex = 0; 71 | this.label1.Text = "源目录"; 72 | // 73 | // tbFolder 74 | // 75 | this.tbFolder.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 76 | | System.Windows.Forms.AnchorStyles.Right))); 77 | this.tbFolder.Location = new System.Drawing.Point(114, 114); 78 | this.tbFolder.Name = "tbFolder"; 79 | this.tbFolder.Size = new System.Drawing.Size(600, 21); 80 | this.tbFolder.TabIndex = 1; 81 | // 82 | // btnFolder 83 | // 84 | this.btnFolder.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 85 | this.btnFolder.Location = new System.Drawing.Point(730, 112); 86 | this.btnFolder.Name = "btnFolder"; 87 | this.btnFolder.Size = new System.Drawing.Size(75, 23); 88 | this.btnFolder.TabIndex = 2; 89 | this.btnFolder.Text = "设置"; 90 | this.btnFolder.UseVisualStyleBackColor = true; 91 | this.btnFolder.Click += new System.EventHandler(this.btnFolder_Click); 92 | // 93 | // label2 94 | // 95 | this.label2.AutoSize = true; 96 | this.label2.Location = new System.Drawing.Point(11, 221); 97 | this.label2.Name = "label2"; 98 | this.label2.Size = new System.Drawing.Size(77, 24); 99 | this.label2.TabIndex = 3; 100 | this.label2.Text = "需处理的文件\r\n扩展名"; 101 | // 102 | // tbExtensions 103 | // 104 | this.tbExtensions.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 105 | | System.Windows.Forms.AnchorStyles.Right))); 106 | this.tbExtensions.Location = new System.Drawing.Point(114, 218); 107 | this.tbExtensions.Multiline = true; 108 | this.tbExtensions.Name = "tbExtensions"; 109 | this.tbExtensions.Size = new System.Drawing.Size(691, 72); 110 | this.tbExtensions.TabIndex = 4; 111 | // 112 | // btnGo 113 | // 114 | this.btnGo.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); 115 | this.btnGo.Location = new System.Drawing.Point(639, 499); 116 | this.btnGo.Name = "btnGo"; 117 | this.btnGo.Size = new System.Drawing.Size(75, 23); 118 | this.btnGo.TabIndex = 5; 119 | this.btnGo.Text = "Go!"; 120 | this.btnGo.UseVisualStyleBackColor = true; 121 | this.btnGo.Click += new System.EventHandler(this.btnGo_Click); 122 | // 123 | // btnAbout 124 | // 125 | this.btnAbout.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); 126 | this.btnAbout.Location = new System.Drawing.Point(730, 499); 127 | this.btnAbout.Name = "btnAbout"; 128 | this.btnAbout.Size = new System.Drawing.Size(75, 23); 129 | this.btnAbout.TabIndex = 6; 130 | this.btnAbout.Text = "关于"; 131 | this.btnAbout.UseVisualStyleBackColor = true; 132 | this.btnAbout.Click += new System.EventHandler(this.btnAbout_Click); 133 | // 134 | // chkSkipBlankLines 135 | // 136 | this.chkSkipBlankLines.AutoSize = true; 137 | this.chkSkipBlankLines.Checked = true; 138 | this.chkSkipBlankLines.CheckState = System.Windows.Forms.CheckState.Checked; 139 | this.chkSkipBlankLines.Location = new System.Drawing.Point(114, 343); 140 | this.chkSkipBlankLines.Name = "chkSkipBlankLines"; 141 | this.chkSkipBlankLines.Size = new System.Drawing.Size(96, 16); 142 | this.chkSkipBlankLines.TabIndex = 7; 143 | this.chkSkipBlankLines.Text = "跳过连续空行"; 144 | this.chkSkipBlankLines.UseVisualStyleBackColor = true; 145 | // 146 | // label3 147 | // 148 | this.label3.AutoSize = true; 149 | this.label3.Location = new System.Drawing.Point(11, 345); 150 | this.label3.Name = "label3"; 151 | this.label3.Size = new System.Drawing.Size(65, 12); 152 | this.label3.TabIndex = 8; 153 | this.label3.Text = "文件处理时"; 154 | // 155 | // chkSkipCommentLines 156 | // 157 | this.chkSkipCommentLines.AutoSize = true; 158 | this.chkSkipCommentLines.Checked = true; 159 | this.chkSkipCommentLines.CheckState = System.Windows.Forms.CheckState.Checked; 160 | this.chkSkipCommentLines.Location = new System.Drawing.Point(250, 345); 161 | this.chkSkipCommentLines.Name = "chkSkipCommentLines"; 162 | this.chkSkipCommentLines.Size = new System.Drawing.Size(108, 16); 163 | this.chkSkipCommentLines.TabIndex = 9; 164 | this.chkSkipCommentLines.Text = "跳过连续注释行"; 165 | this.chkSkipCommentLines.UseVisualStyleBackColor = true; 166 | // 167 | // picBanner 168 | // 169 | this.picBanner.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 170 | | System.Windows.Forms.AnchorStyles.Right))); 171 | this.picBanner.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("picBanner.BackgroundImage"))); 172 | this.picBanner.Location = new System.Drawing.Point(0, 0); 173 | this.picBanner.Name = "picBanner"; 174 | this.picBanner.Size = new System.Drawing.Size(818, 83); 175 | this.picBanner.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; 176 | this.picBanner.TabIndex = 10; 177 | this.picBanner.TabStop = false; 178 | // 179 | // lblApp 180 | // 181 | this.lblApp.AutoSize = true; 182 | this.lblApp.BackColor = System.Drawing.Color.Transparent; 183 | this.lblApp.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 184 | this.lblApp.ForeColor = System.Drawing.Color.White; 185 | this.lblApp.Location = new System.Drawing.Point(22, 25); 186 | this.lblApp.Name = "lblApp"; 187 | this.lblApp.Size = new System.Drawing.Size(180, 27); 188 | this.lblApp.TabIndex = 11; 189 | this.lblApp.Text = "File Counter"; 190 | // 191 | // label5 192 | // 193 | this.label5.AutoSize = true; 194 | this.label5.BackColor = System.Drawing.Color.Transparent; 195 | this.label5.Location = new System.Drawing.Point(338, 256); 196 | this.label5.Name = "label5"; 197 | this.label5.Size = new System.Drawing.Size(0, 12); 198 | this.label5.TabIndex = 12; 199 | // 200 | // lblVersion 201 | // 202 | this.lblVersion.AutoSize = true; 203 | this.lblVersion.ForeColor = System.Drawing.Color.White; 204 | this.lblVersion.Location = new System.Drawing.Point(216, 39); 205 | this.lblVersion.Name = "lblVersion"; 206 | this.lblVersion.Size = new System.Drawing.Size(23, 12); 207 | this.lblVersion.TabIndex = 13; 208 | this.lblVersion.Text = "1.0"; 209 | // 210 | // openFileDialog1 211 | // 212 | this.openFileDialog1.FileName = "openFileDialog"; 213 | // 214 | // label7 215 | // 216 | this.label7.AutoSize = true; 217 | this.label7.Location = new System.Drawing.Point(11, 418); 218 | this.label7.Name = "label7"; 219 | this.label7.Size = new System.Drawing.Size(53, 12); 220 | this.label7.TabIndex = 14; 221 | this.label7.Text = "输出行数"; 222 | // 223 | // tbLines 224 | // 225 | this.tbLines.Location = new System.Drawing.Point(114, 412); 226 | this.tbLines.Maximum = new decimal(new int[] { 227 | 100000, 228 | 0, 229 | 0, 230 | 0}); 231 | this.tbLines.Name = "tbLines"; 232 | this.tbLines.Size = new System.Drawing.Size(120, 21); 233 | this.tbLines.TabIndex = 15; 234 | this.tbLines.Value = new decimal(new int[] { 235 | 9000, 236 | 0, 237 | 0, 238 | 0}); 239 | // 240 | // tbOutFile 241 | // 242 | this.tbOutFile.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 243 | | System.Windows.Forms.AnchorStyles.Right))); 244 | this.tbOutFile.Location = new System.Drawing.Point(114, 375); 245 | this.tbOutFile.Name = "tbOutFile"; 246 | this.tbOutFile.Size = new System.Drawing.Size(600, 21); 247 | this.tbOutFile.TabIndex = 17; 248 | this.tbOutFile.Text = "out.txt"; 249 | // 250 | // label4 251 | // 252 | this.label4.AutoSize = true; 253 | this.label4.Location = new System.Drawing.Point(11, 381); 254 | this.label4.Name = "label4"; 255 | this.label4.Size = new System.Drawing.Size(53, 12); 256 | this.label4.TabIndex = 16; 257 | this.label4.Text = "输出文件"; 258 | // 259 | // chkOpenWhenFinished 260 | // 261 | this.chkOpenWhenFinished.AutoSize = true; 262 | this.chkOpenWhenFinished.Checked = true; 263 | this.chkOpenWhenFinished.CheckState = System.Windows.Forms.CheckState.Checked; 264 | this.chkOpenWhenFinished.Location = new System.Drawing.Point(114, 449); 265 | this.chkOpenWhenFinished.Name = "chkOpenWhenFinished"; 266 | this.chkOpenWhenFinished.Size = new System.Drawing.Size(15, 14); 267 | this.chkOpenWhenFinished.TabIndex = 18; 268 | this.chkOpenWhenFinished.UseVisualStyleBackColor = true; 269 | // 270 | // btnFile 271 | // 272 | this.btnFile.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 273 | this.btnFile.Location = new System.Drawing.Point(730, 374); 274 | this.btnFile.Name = "btnFile"; 275 | this.btnFile.Size = new System.Drawing.Size(75, 23); 276 | this.btnFile.TabIndex = 19; 277 | this.btnFile.Text = "设置"; 278 | this.btnFile.UseVisualStyleBackColor = true; 279 | this.btnFile.Click += new System.EventHandler(this.btnFile_Click); 280 | // 281 | // tbEncoding 282 | // 283 | this.tbEncoding.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 284 | | System.Windows.Forms.AnchorStyles.Right))); 285 | this.tbEncoding.Location = new System.Drawing.Point(114, 306); 286 | this.tbEncoding.Name = "tbEncoding"; 287 | this.tbEncoding.Size = new System.Drawing.Size(691, 21); 288 | this.tbEncoding.TabIndex = 21; 289 | this.tbEncoding.Text = "UTF8"; 290 | // 291 | // label6 292 | // 293 | this.label6.AutoSize = true; 294 | this.label6.Location = new System.Drawing.Point(11, 310); 295 | this.label6.Name = "label6"; 296 | this.label6.Size = new System.Drawing.Size(41, 12); 297 | this.label6.TabIndex = 20; 298 | this.label6.Text = "字符集"; 299 | // 300 | // tbSkipSubFolders 301 | // 302 | this.tbSkipSubFolders.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 303 | | System.Windows.Forms.AnchorStyles.Right))); 304 | this.tbSkipSubFolders.Location = new System.Drawing.Point(114, 151); 305 | this.tbSkipSubFolders.Name = "tbSkipSubFolders"; 306 | this.tbSkipSubFolders.Size = new System.Drawing.Size(691, 21); 307 | this.tbSkipSubFolders.TabIndex = 23; 308 | this.tbSkipSubFolders.Text = "obj,bin,images"; 309 | // 310 | // label8 311 | // 312 | this.label8.AutoSize = true; 313 | this.label8.Location = new System.Drawing.Point(11, 155); 314 | this.label8.Name = "label8"; 315 | this.label8.Size = new System.Drawing.Size(65, 12); 316 | this.label8.TabIndex = 22; 317 | this.label8.Text = "跳过子目录"; 318 | // 319 | // chkSkipHidden 320 | // 321 | this.chkSkipHidden.AutoSize = true; 322 | this.chkSkipHidden.Checked = true; 323 | this.chkSkipHidden.CheckState = System.Windows.Forms.CheckState.Checked; 324 | this.chkSkipHidden.Location = new System.Drawing.Point(114, 188); 325 | this.chkSkipHidden.Name = "chkSkipHidden"; 326 | this.chkSkipHidden.Size = new System.Drawing.Size(15, 14); 327 | this.chkSkipHidden.TabIndex = 24; 328 | this.chkSkipHidden.UseVisualStyleBackColor = true; 329 | // 330 | // label9 331 | // 332 | this.label9.AutoSize = true; 333 | this.label9.Location = new System.Drawing.Point(11, 187); 334 | this.label9.Name = "label9"; 335 | this.label9.Size = new System.Drawing.Size(77, 12); 336 | this.label9.TabIndex = 25; 337 | this.label9.Text = "跳过隐藏目录"; 338 | // 339 | // label10 340 | // 341 | this.label10.AutoSize = true; 342 | this.label10.Location = new System.Drawing.Point(12, 451); 343 | this.label10.Name = "label10"; 344 | this.label10.Size = new System.Drawing.Size(65, 12); 345 | this.label10.TabIndex = 26; 346 | this.label10.Text = "完成时打开"; 347 | // 348 | // FormMain 349 | // 350 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 351 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 352 | this.ClientSize = new System.Drawing.Size(817, 534); 353 | this.Controls.Add(this.label10); 354 | this.Controls.Add(this.label9); 355 | this.Controls.Add(this.chkSkipHidden); 356 | this.Controls.Add(this.tbSkipSubFolders); 357 | this.Controls.Add(this.label8); 358 | this.Controls.Add(this.tbEncoding); 359 | this.Controls.Add(this.label6); 360 | this.Controls.Add(this.btnFile); 361 | this.Controls.Add(this.chkOpenWhenFinished); 362 | this.Controls.Add(this.tbOutFile); 363 | this.Controls.Add(this.label4); 364 | this.Controls.Add(this.tbLines); 365 | this.Controls.Add(this.label7); 366 | this.Controls.Add(this.lblVersion); 367 | this.Controls.Add(this.label5); 368 | this.Controls.Add(this.lblApp); 369 | this.Controls.Add(this.picBanner); 370 | this.Controls.Add(this.chkSkipCommentLines); 371 | this.Controls.Add(this.label3); 372 | this.Controls.Add(this.chkSkipBlankLines); 373 | this.Controls.Add(this.btnAbout); 374 | this.Controls.Add(this.btnGo); 375 | this.Controls.Add(this.tbExtensions); 376 | this.Controls.Add(this.label2); 377 | this.Controls.Add(this.btnFolder); 378 | this.Controls.Add(this.tbFolder); 379 | this.Controls.Add(this.label1); 380 | this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); 381 | this.Name = "FormMain"; 382 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 383 | this.Text = "FileCounter"; 384 | this.Load += new System.EventHandler(this.FormMain_Load); 385 | ((System.ComponentModel.ISupportInitialize)(this.picBanner)).EndInit(); 386 | ((System.ComponentModel.ISupportInitialize)(this.tbLines)).EndInit(); 387 | this.ResumeLayout(false); 388 | this.PerformLayout(); 389 | 390 | } 391 | 392 | #endregion 393 | 394 | private System.Windows.Forms.Label label1; 395 | private System.Windows.Forms.TextBox tbFolder; 396 | private System.Windows.Forms.Button btnFolder; 397 | private System.Windows.Forms.Label label2; 398 | private System.Windows.Forms.TextBox tbExtensions; 399 | private System.Windows.Forms.Button btnGo; 400 | private System.Windows.Forms.Button btnAbout; 401 | private System.Windows.Forms.CheckBox chkSkipBlankLines; 402 | private System.Windows.Forms.Label label3; 403 | private System.Windows.Forms.CheckBox chkSkipCommentLines; 404 | private System.Windows.Forms.PictureBox picBanner; 405 | private System.Windows.Forms.Label lblApp; 406 | private System.Windows.Forms.Label label5; 407 | private System.Windows.Forms.Label lblVersion; 408 | private System.Windows.Forms.OpenFileDialog openFileDialog1; 409 | private System.Windows.Forms.Label label7; 410 | private System.Windows.Forms.NumericUpDown tbLines; 411 | private System.Windows.Forms.TextBox tbOutFile; 412 | private System.Windows.Forms.Label label4; 413 | private System.Windows.Forms.CheckBox chkOpenWhenFinished; 414 | private System.Windows.Forms.Button btnFile; 415 | private System.Windows.Forms.TextBox tbEncoding; 416 | private System.Windows.Forms.Label label6; 417 | private System.Windows.Forms.TextBox tbSkipSubFolders; 418 | private System.Windows.Forms.Label label8; 419 | private System.Windows.Forms.CheckBox chkSkipHidden; 420 | private System.Windows.Forms.Label label9; 421 | private System.Windows.Forms.Label label10; 422 | } 423 | } -------------------------------------------------------------------------------- /FormMain.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Configuration; 5 | using System.Data; 6 | using System.Drawing; 7 | using System.Linq; 8 | using System.Reflection; 9 | using System.Text; 10 | using System.Threading.Tasks; 11 | using System.Windows.Forms; 12 | 13 | namespace FileCounter 14 | { 15 | public partial class FormMain : Form 16 | { 17 | public FormMain() 18 | { 19 | InitializeComponent(); 20 | } 21 | 22 | private void FormMain_Load(object sender, EventArgs e) 23 | { 24 | this.lblApp.BackColor = Color.Transparent; 25 | this.lblApp.Parent = picBanner; 26 | this.lblVersion.Text = Helper.GetVersion(); 27 | this.lblVersion.BackColor = Color.Transparent; 28 | this.lblVersion.Parent = picBanner; 29 | 30 | // 31 | var cfg = Config.ReadFromAppConfig(); 32 | this.tbEncoding.Text = cfg.Encoding; 33 | this.tbExtensions.Text = cfg.Extensions.ToSeparatedString(); 34 | this.tbSkipSubFolders.Text = cfg.SkipSubFolders.ToSeparatedString(); 35 | this.chkSkipHidden.Checked = cfg.SkipHidden; 36 | this.chkSkipBlankLines.Checked = cfg.SkipBlankLines; 37 | this.chkSkipCommentLines.Checked = cfg.SkipCommentLines; 38 | this.chkOpenWhenFinished.Checked = cfg.OpenWhenFinished; 39 | } 40 | 41 | // 设置源程序目录 42 | private void btnFolder_Click(object sender, EventArgs e) 43 | { 44 | FolderBrowserDialog dlg = new FolderBrowserDialog(); 45 | dlg.SelectedPath = Application.StartupPath + "\\test\\"; 46 | if (dlg.ShowDialog() == DialogResult.OK) 47 | this.tbFolder.Text = dlg.SelectedPath; 48 | } 49 | 50 | 51 | // 设置输出文件路径 52 | private void btnFile_Click(object sender, EventArgs e) 53 | { 54 | SaveFileDialog dlg = new SaveFileDialog(); 55 | if (!tbOutFile.Text.IsEmpty()) 56 | { 57 | System.IO.FileInfo fi = new System.IO.FileInfo(this.tbOutFile.Text); 58 | dlg.InitialDirectory = fi.DirectoryName; 59 | dlg.FileName = fi.Name; 60 | } 61 | if (dlg.ShowDialog() == DialogResult.OK) 62 | this.tbOutFile.Text = dlg.FileName; 63 | } 64 | 65 | // 显示关于 66 | private void btnAbout_Click(object sender, EventArgs e) 67 | { 68 | var about = ConfigurationManager.AppSettings["About"]; 69 | MessageBox.Show(about, "关于"); 70 | } 71 | 72 | // 启动控制台进行输出运算 73 | private void btnGo_Click(object sender, EventArgs e) 74 | { 75 | var cfg = new Config(); 76 | cfg.RootFolder = this.tbFolder.Text; 77 | cfg.Encoding = this.tbEncoding.Text; 78 | cfg.Extensions = this.tbExtensions.Text.ToArray(); 79 | cfg.SkipBlankLines = this.chkSkipBlankLines.Checked; 80 | cfg.SkipCommentLines = this.chkSkipCommentLines.Checked; 81 | cfg.SkipSubFolders = this.tbSkipSubFolders.Text.ToArray(); 82 | cfg.SkipHidden = this.chkSkipHidden.Checked; 83 | cfg.OpenWhenFinished = this.chkOpenWhenFinished.Checked; 84 | cfg.OutFile = this.tbOutFile.Text; 85 | cfg.OutLines = (int)this.tbLines.Value; 86 | Scanner.Run(cfg); 87 | } 88 | 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /Helper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace FileCounter 10 | { 11 | /// 12 | /// 静态辅助方法 13 | /// 14 | static class Helper 15 | { 16 | /// 获取程序版本号 17 | public static string GetVersion() 18 | { 19 | var v = Assembly.GetExecutingAssembly().GetName().Version; 20 | return string.Format("{0}.{1}.{2}", v.Major, v.Minor, v.Revision); 21 | } 22 | 23 | /// 文本拆分成数组 24 | public static string[] ToArray(this string text) 25 | { 26 | return text.Replace(" ", "") 27 | .Replace("\r", "") 28 | .Replace("\n", "") 29 | .Replace("\t", "") 30 | .Split('|', ',', ';'); 31 | } 32 | 33 | /// 文本为空 34 | public static bool IsEmpty(this string text) 35 | { 36 | return string.IsNullOrEmpty(text); 37 | } 38 | 39 | /// 转化为布尔 40 | public static bool ToBool(this string text) 41 | { 42 | if (!text.IsEmpty() && text.ToLower() == "true") 43 | return true; 44 | return false; 45 | } 46 | 47 | 48 | /// 文本是注释行 49 | public static bool IsCommentLine(this string text) 50 | { 51 | if (text.IsEmpty()) 52 | return false; 53 | text = text.TrimStart(); 54 | return (text.StartsWith("//") || text.StartsWith("#")); 55 | } 56 | 57 | /// 转化为逗号分隔的字符串 58 | public static string ToSeparatedString(this IEnumerable source, char seperator = ',') 59 | { 60 | if (source == null) 61 | return ""; 62 | string txt = ""; 63 | foreach (var item in source) 64 | txt += item.ToString() + seperator; 65 | return txt.TrimEnd(seperator); 66 | } 67 | 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Images/console.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/surfsky/FileCounter/077fc6aa2f1adcc4043fbe5aaeb99ea6173860f3/Images/console.png -------------------------------------------------------------------------------- /Images/form.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/surfsky/FileCounter/077fc6aa2f1adcc4043fbe5aaeb99ea6173860f3/Images/form.png -------------------------------------------------------------------------------- /Logger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FileCounter 4 | { 5 | internal static class Logger 6 | { 7 | public static void Log(string format, params object[] parameters) 8 | { 9 | Console.WriteLine(format, parameters); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.IO; 6 | using System.Configuration; 7 | using System.Windows.Forms; 8 | 9 | namespace FileCounter 10 | { 11 | 12 | class Program 13 | { 14 | [STAThread] 15 | static void Main(string[] args) 16 | { 17 | Logger.Log("-----------------------------------------"); 18 | Logger.Log("FileCounter"); 19 | Logger.Log("Version {0}", Helper.GetVersion()); 20 | Logger.Log("https://www.github.com/surfsky/"); 21 | Logger.Log("2024-04"); 22 | Logger.Log("-----------------------------------------"); 23 | 24 | 25 | if (args.Length == 0) 26 | Application.Run(new FormMain()); 27 | else 28 | { 29 | var cfg = Config.ReadFromAppConfig(); 30 | cfg.RootFolder = args[0]; 31 | cfg.OutFile = args[1]; 32 | cfg.OutLines = Convert.ToInt32(args[2]); 33 | Scanner.Run(cfg); 34 | } 35 | } 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的常规信息通过以下 6 | // 特性集控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("LineCounter")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("LineCounter")] 13 | [assembly: AssemblyCopyright("Copyright © 2011")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 使此程序集中的类型 18 | // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型, 19 | // 则将该类型上的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("0dc1dadf-3f7c-4f02-a137-89496f50ddfa")] 24 | 25 | // 程序集的版本信息由下面四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 内部版本号 30 | // 修订号 31 | // 32 | // 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("2.0.*")] 36 | [assembly: AssemblyFileVersion("2.0.0.0")] 37 | -------------------------------------------------------------------------------- /Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 此代码由工具生成。 4 | // 运行时版本:4.0.30319.42000 5 | // 6 | // 对此文件的更改可能会导致不正确的行为,并且如果 7 | // 重新生成代码,这些更改将会丢失。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace FileCounter.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// 一个强类型的资源类,用于查找本地化的字符串等。 17 | /// 18 | // 此类是由 StronglyTypedResourceBuilder 19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 20 | // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen 21 | // (以 /str 作为命令选项),或重新生成 VS 项目。 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// 返回此类使用的缓存的 ResourceManager 实例。 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("FileCounter.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// 重写当前线程的 CurrentUICulture 属性,对 51 | /// 使用此强类型资源类的所有资源查找执行重写。 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 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 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FileCounter 2 | 3 | - 源码统计及合并工具,可用于著作权申报 4 | - 作者:https://www.github.com/surfsky/ 5 | 6 | 7 | # 功能 8 | 9 | - 可统计各种文件的数目和代码行数 10 | - 合并源码 11 | - 可设置文本编码方式 12 | - 可控制输出行数 13 | - 可跳过连续空行 14 | - 可跳过连续注释行 15 | 16 | 17 | # 窗口方式运行 18 | 19 | - 直接双击 FileMerger.exe 20 | - 按照页面提示操作 21 | ![](https://github.com/surfsky/FileMerger/blob/master/Images/form.png) 22 | 23 | # 控制台方式运行 24 | 25 | ``` 26 | FileMerger.exe .\Test\ out.txt 9000 27 | ``` 28 | 29 | - 参数1:源代码目录 30 | - 参数2:输出文件路径 31 | - 参数3:输出文件行数 32 | ![](https://github.com/surfsky/FileMerger/blob/master/Images/console.png) 33 | 34 | 35 | # Tasks 36 | 37 | - 列出文件勾选后再导出,更可控 38 | - 排序方式、深度优先、广度优先 39 | 40 | # History 41 | 42 | 2.0 43 | - 更名为 FileCounter。 44 | - 类库迁移到 NetFramework 4.8。 45 | - 可跳过一些不需要统计的子目录。 46 | - 可跳过隐藏目录。 47 | - 修改导出后的文件名,用相对路径,并用注释行分隔。 48 | - 修改默认统计文件,跳过一些配置类的文件。 49 | - 优化目录和文件排序,并广度优先检索。 50 | 51 | 1.0 52 | - 可统计各种文件的数目和代码行数 53 | - 合并源码 54 | - 可设置文本编码方式 55 | - 可控制输出行数 56 | - 可跳过连续空行 57 | - 可跳过连续注释行 58 | -------------------------------------------------------------------------------- /Scanner.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Windows.Forms; 7 | 8 | namespace FileCounter 9 | { 10 | /// 统计数据 11 | public class Stat 12 | { 13 | public int Files; // 文件数 14 | public int Lines; // 代码行数 15 | } 16 | 17 | 18 | /// 19 | /// 扫描统计器 20 | /// 21 | public static class Scanner 22 | { 23 | // 计数字典(文件类型-文件数-代码行数) 24 | static Dictionary _counter = new Dictionary(); 25 | 26 | // console 27 | public static void Run(Config cfg) 28 | { 29 | // 参数校验 30 | var root = cfg.RootFolder; 31 | var outFile = cfg.OutFile; 32 | if (root.IsEmpty()) 33 | { 34 | MessageBox.Show("请录入源代码目录", "错误"); 35 | return; 36 | } 37 | 38 | // 处理 39 | long totleLines = 0; 40 | StreamWriter sw = (outFile == null) ? null : new StreamWriter(outFile); 41 | Logger.Log(ConfigurationManager.AppSettings["about"]); 42 | Logger.Log("{0}", System.DateTime.Now); 43 | ProcessFolder(cfg, root, ref totleLines, ref sw); 44 | if (sw != null) 45 | sw.Close(); 46 | 47 | // 显示统计信息 48 | int files, lines; 49 | GetTotalFiles(out files, out lines); 50 | Logger.Log(""); 51 | Logger.Log("-----------------------------------------"); 52 | Logger.Log("-- " + root); 53 | Logger.Log("-----------------------------------------"); 54 | Logger.Log("类别\t文件数\t代码行数\t平均"); 55 | Logger.Log("----\t------\t--------\t--------"); 56 | foreach (string key in _counter.Keys) 57 | { 58 | Stat cnt = _counter[key]; 59 | Logger.Log("{0}\t{1}\t{2}\t{3}", key, cnt.Files, cnt.Lines, GetAverage(cnt.Lines, cnt.Files)); 60 | } 61 | 62 | Logger.Log("----\t------\t--------"); 63 | Logger.Log("合计\t{0}\t{1}\t{2}", files, lines, GetAverage(lines, files)); 64 | Logger.Log(""); 65 | Logger.Log("{0}", System.DateTime.Now); 66 | Logger.Log("{0}", root); 67 | 68 | // 打开输出文件 69 | if (cfg.OpenWhenFinished && !cfg.OutFile.IsEmpty()) 70 | System.Diagnostics.Process.Start(cfg.OutFile); 71 | } 72 | 73 | static double GetAverage(int lines, int files) 74 | { 75 | if (files == 0) return 0; 76 | //else return lines*1.0 / files; 77 | else return Math.Round((double)(lines * 1.0 / files)); 78 | } 79 | 80 | // 统计 81 | static void GetTotalFiles(out int files, out int lines) 82 | { 83 | files = 0; 84 | lines = 0; 85 | foreach (string key in _counter.Keys) 86 | { 87 | Stat cnt = _counter[key]; 88 | files += cnt.Files; 89 | lines += cnt.Lines; 90 | } 91 | } 92 | 93 | 94 | // 处理单个文本文件 95 | static int ProcessFile(Config cfg, string filePath, ref long totleLines, ref StreamWriter sw) 96 | { 97 | int lines = 0; 98 | int blankLines = 0; 99 | int commentLines = 0; 100 | var relativePath = GetRelativePath(cfg.RootFolder, filePath); 101 | WriteLineToFile(cfg, sw, @"//////////////////////////////////////////////////////", totleLines); 102 | WriteLineToFile(cfg, sw, @"// " + relativePath, totleLines); 103 | WriteLineToFile(cfg, sw, @"//////////////////////////////////////////////////////", totleLines); 104 | using (StreamReader sr = new StreamReader(filePath, cfg.Enc)) 105 | { 106 | while (!sr.EndOfStream) 107 | { 108 | lines++; 109 | string line = sr.ReadLine(); 110 | 111 | // 跳过连续空行;跳过连续备注行 112 | blankLines = !line.IsEmpty() ? 0 : blankLines + 1; 113 | commentLines = !line.IsCommentLine() ? 0 : commentLines + 1; 114 | if (cfg.SkipBlankLines && blankLines > 1) continue; 115 | if (cfg.SkipCommentLines && commentLines > 1) continue; 116 | 117 | // 118 | WriteLineToFile(cfg, sw, line, totleLines); 119 | } 120 | } 121 | WriteLineToFile(cfg, sw, "", totleLines); 122 | WriteLineToFile(cfg, sw, "", totleLines); 123 | totleLines += lines; 124 | return lines; 125 | } 126 | 127 | /// 获取相对路径 128 | /// 根目录 129 | /// 文件完整目录 130 | private static string GetRelativePath(string rootFolder, string filePath) 131 | { 132 | return filePath.Substring(rootFolder.Length); 133 | } 134 | 135 | // 处理一个目录 136 | static void ProcessFolder(Config cfg, string folder, ref long totleLines, ref StreamWriter sw) 137 | { 138 | // 目录校验 139 | if (!Directory.Exists(folder)) 140 | return; 141 | 142 | // 跳过目录 143 | DirectoryInfo di = new DirectoryInfo(folder); 144 | //var relativeFolder = folder.Substring(cfg.RootFolder.Length).ToLower().TrimStart('\\', '/'); 145 | if (cfg.SkipSubFolders.Contains(di.Name.ToLower())) 146 | { 147 | Logger.Log("跳过忽略目录 {0}", folder); 148 | return; 149 | } 150 | if (cfg.SkipHidden && di.Attributes.HasFlag(FileAttributes.Hidden)) 151 | { 152 | Logger.Log("跳过隐藏目录 {0}", folder); 153 | return; 154 | } 155 | 156 | 157 | // 处理当前目录文件 158 | Logger.Log("-----------------------------------------"); 159 | Logger.Log("处理目录 {0}", di.FullName); 160 | var files = di.GetFiles().OrderBy(t => t.Name).ToList(); 161 | foreach (FileInfo fi in files) 162 | { 163 | // 扩展名(去掉前面的点) 164 | string ext = fi.Extension.ToLower(); 165 | if (ext.Length > 0) 166 | ext = ext.Remove(0, 1); 167 | if (ext == "" || !cfg.Extensions.Contains(ext)) 168 | { 169 | Logger.Log("跳过 {0}", fi.FullName); 170 | continue; 171 | } 172 | 173 | // 文件行数 174 | int line = ProcessFile(cfg, fi.FullName, ref totleLines, ref sw); 175 | 176 | // 统计文件数和行数 177 | if (!_counter.Keys.Contains(ext)) 178 | _counter.Add(ext, new Stat()); 179 | _counter[ext].Files += 1; 180 | _counter[ext].Lines += line; 181 | Logger.Log("行数 {0} {1}", line, fi.FullName); 182 | } 183 | 184 | // 递归子目录 185 | var dirs = di.GetDirectories().OrderBy(t => t.FullName).ToList(); 186 | foreach (DirectoryInfo info in dirs) 187 | ProcessFolder(cfg, info.FullName, ref totleLines, ref sw); 188 | } 189 | 190 | /// 输出一行文本(如果未超出总行数限制) 191 | private static void WriteLineToFile(Config cfg, StreamWriter sw, string line, long totleCnt) 192 | { 193 | if (sw != null && totleCnt < cfg.OutLines) 194 | sw.WriteLine(line); 195 | } 196 | } 197 | } -------------------------------------------------------------------------------- /bin/Debug/FileCounter.exe.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /bin/Debug/FileCounter.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/surfsky/FileCounter/077fc6aa2f1adcc4043fbe5aaeb99ea6173860f3/bin/Debug/FileCounter.pdb -------------------------------------------------------------------------------- /bin/Debug/Test/Config.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace FileMerger 7 | { 8 | 9 | 10 | 11 | 12 | /// 13 | /// 运行参数 14 | /// 15 | public class Config 16 | { 17 | // 源控制 18 | public string Folder { get; set; } 19 | public string[] Extensions { get; set; } 20 | 21 | 22 | // 辅助参数 23 | // 24 | public bool SkipBlankLines { get; set; } 25 | public bool SkipCommentLines { get; set; } 26 | public bool OpenWhenFinished { get; set; } 27 | 28 | // 输出控制 29 | public string OutFile { get; set; } 30 | public int OutLines { get; set; } = 9000; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /bin/Debug/test.bat: -------------------------------------------------------------------------------- 1 | FileMerger.exe .\Test out.txt 9000 2 | 3 | --------------------------------------------------------------------------------