前言:程序介绍
这个项目的应用主要是统计源代码中的总行数,代码行数,空格行数,注释行数,首先主函数放在program.cs程序中,主函数主要是设计了选择功能,通过获取文件的路径进行选择统计单个文件,文件夹,或是指定文件类型的文件夹,在统计的过程中根据选择的文件类型,如java类 cs类进行调用不同的程序,如果是java类文件就调用javacount.cs程序进行统计,如果选择的是cs类文件则调用cshareCount.cs程序进行统计,其中调用的Count.cs定义了一个统计的类,包含了所有要统计的内容,并设置了过滤函数,替换内容,以方便统计,FileHelper.cs程序用来罗列文件,获取文件内容和路径,以及新建,保存和删除文件,WorldCountFactory.cs程序用来统计各类文件类型,Blankcount.cs用来统计每个程序里面的空格行。
</pre><p> <strong><span style="font-size:12px;color:#ff0000;"> Program.cs</span>///这个程序为主程序,用来定义选择功能,选择统计单个文件,文件夹,指定类型的文件夹来调用其他程序统计源代码</strong></p><pre class="csharp" name="code">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorldCount
{
class Program
{
//主函数
static void Main(string[] args)
{
Console.WriteLine("请选择功能,\n1、统计单个文件\n2、统计文件夹\n3、统计文件夹并指定类型");
int type = Convert.ToInt32(Console.ReadLine());
//开始统计
StartCount(type);
Console.WriteLine("请输入任意键继续");
Console.ReadKey();
}
//定义开始统计的函数
private static void StartCount(int type)
{
//功能分支选择
switch (type)
{
case 1:
Console.WriteLine("请输入文件路径");
String path = Console.ReadLine();
Count(path);
break;
case 2:
Console.WriteLine("请输入文件夹路径");
String folderPath = Console.ReadLine();
CountFolder(folderPath);
break;
case 3:
Console.WriteLine("请输入文件夹路径");
String folderPath1 = Console.ReadLine();
Console.WriteLine("请输入文件后缀名");
String typeName = Console.ReadLine();
CountFolder(folderPath1, typeName);
break;
default:
break;
}
}
/// <summary>
/// 统计单个文件
/// </summary>
/// <param name="filePath"></param>
private static void Count(String filePath)
{
String type = FileHelper.GetType(filePath);
Count count = WorldCountFactory.CreateCount(type);
String fileContent = FileHelper.GetFileContent(filePath);
count.SetFileContent(fileContent);
int allLineCount = count.GetAllLineCount();
int codeLineCount = count.GetCodeLineCount();
int blankLineCount = count.GetBlankLineCount();
int commentLineCount = count.GetCommentLineCount();
PrintCount(allLineCount, blankLineCount, commentLineCount, codeLineCount);
}
/// <summary>
/// 统计文件夹
/// </summary>
/// <param name="folderPath"></param>
private static void CountFolder(String folderPath)
{
FileHelper fh = new FileHelper();
fh.GetChildsFile(folderPath);
int allLineCount = 0;
int codeLineCount = 0;
int blankLineCount = 0;
int commentLineCount = 0;
foreach (String filePath in fh.fileList)
{
//统计
String type = FileHelper.GetType(filePath);
Count count = WorldCountFactory.CreateCount(type);
String fileContent = FileHelper.GetFileContent(filePath);
count.SetFileContent(fileContent);
allLineCount += count.GetAllLineCount();
codeLineCount += count.GetCodeLineCount();
blankLineCount += count.GetBlankLineCount();
commentLineCount += count.GetCommentLineCount();
}
PrintCount(allLineCount, blankLineCount, commentLineCount, codeLineCount);
}
/// <summary>
/// 统计文件夹并指定类型
/// </summary>
/// <param name="folderPath"></param>
/// <param name="typeName"></param>
private static void CountFolder(String folderPath, String typeName)
{
FileHelper fh = new FileHelper();
fh.GetChildsFile(folderPath);
int allLineCount = 0;
int codeLineCount = 0;
int blankLineCount = 0;
int commentLineCount = 0;
foreach (String filePath in fh.fileList)
{
//统计
String type = FileHelper.GetType(filePath);
if (type == typeName)
{
Count count = WorldCountFactory.CreateCount(type);
String fileContent = FileHelper.GetFileContent(filePath);
count.SetFileContent(fileContent);
allLineCount += count.GetAllLineCount();
codeLineCount += count.GetCodeLineCount();
blankLineCount += count.GetBlankLineCount();
commentLineCount += count.GetCommentLineCount();
}
}
PrintCount(allLineCount, blankLineCount, commentLineCount, codeLineCount);
}
private static void PrintCount(int allLineCount, int blankLineCount, int commentLineCount, int codeLineCount)
{
Console.WriteLine("总行数为{0}\n空白行数为{1}\n注释行数为{2}\n代码行数为{3}", allLineCount, blankLineCount, commentLineCount, codeLineCount);
}
}
}
Count.cs
///该程序主要是定义一个统计的类,包含了所有要统计的内容,并设置了过滤函数,替换内容,方便统计
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorldCount
{
abstract class Count
{
/// <summary>
/// 替换字符串
/// </summary>
protected const String REPLACE_STRING = "WorldCount";
/// <summary>
/// 多行注释替换字符
/// </summary>
protected const String COMMMENT_REPLACE_STRING = "//WorldCount";
/// <summary>
/// 统计内容
/// </summary>
protected String fileContent;
/// <summary>
/// 代码行数
/// </summary>
protected int codeLineCount;
/// <summary>
/// 总行数
/// </summary>
protected int allLineCount;
/// <summary>
/// 空白行数
/// </summary>
protected int blankLineCount;
/// <summary>
/// 注释行数
/// </summary>
protected int commentLineCount;
/// <summary>
/// 设置需进行统计内容
/// </summary>
/// <param name="fileContent"></param>
public void SetFileContent(String fileContent)
{
this.fileContent = FilterContent(fileContent);
}
/// <summary>
/// 过滤清除无效字符
/// </summary>
/// <param name="fileContent"></param>
/// <returns></returns>
protected abstract String FilterContent(String fileContent);
/// <summary>
/// 替换内容,解除干扰
/// </summary>
/// <param name="fileContent"></param>
/// <param name="arrString"></param>
/// <param name="replaceString"></param>
/// <returns></returns>
protected String Replace(String fileContent, String arrString, String replaceString)
{
String[] arr = arrString.Split('\n');
foreach (string str in arr)
{
fileContent = fileContent.Replace(str, replaceString);
}
return fileContent;
}
/// <summary>
/// 代码行数
/// </summary>
public abstract int GetCodeLineCount();
/// <summary>
/// 总行数
/// </summary>
public abstract int GetAllLineCount();
/// <summary>
/// 空白行数
/// </summary>
public abstract int GetBlankLineCount();
/// <summary>
/// 注释行数
/// </summary>
public abstract int GetCommentLineCount();
}
}
WorldCountFactory.cs
///这个函数用来调用要统计的各种文件类型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorldCount
{
//定义统计文件类型的总类
class WorldCountFactory
{
//声明创建统计函数
public static Count CreateCount(String type)
{
//java类文件时,返回到javaCount()函数
if (type == "java")
{
return new JavaCount();
}
//cs类文件时,返回到cshareCount()函数
if (type == "cs")
{
return new CShareCount();
}
//无文件时,返回到BlankCount()函数
return new BlankCount();
}
}
}
Count.cs
///该程序主要是定义一个统计的类,包含了所有要统计的内容,并设置了过滤函数,替换内容,方便统计
<strong>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorldCount
{
abstract class Count
{
/// <summary>
/// 替换字符串
/// </summary>
protected const String REPLACE_STRING = "WorldCount";
/// <summary>
/// 多行注释替换字符
/// </summary>
protected const String COMMMENT_REPLACE_STRING = "//WorldCount";
/// <summary>
/// 统计内容
/// </summary>
protected String fileContent;
/// <summary>
/// 代码行数
/// </summary>
protected int codeLineCount;
/// <summary>
/// 总行数
/// </summary>
protected int allLineCount;
/// <summary>
/// 空白行数
/// </summary>
protected int blankLineCount;
/// <summary>
/// 注释行数
/// </summary>
protected int commentLineCount;
/// <summary>
/// 设置需进行统计内容
/// </summary>
/// <param name="fileContent"></param>
public void SetFileContent(String fileContent)
{
this.fileContent = FilterContent(fileContent);
}
/// <summary>
/// 过滤清除无效字符
/// </summary>
/// <param name="fileContent"></param>
/// <returns></returns>
protected abstract String FilterContent(String fileContent);
/// <summary>
/// 替换内容,解除干扰
/// </summary>
/// <param name="fileContent"></param>
/// <param name="arrString"></param>
/// <param name="replaceString"></param>
/// <returns></returns>
protected String Replace(String fileContent, String arrString, String replaceString)
{
String[] arr = arrString.Split('\n');
foreach (string str in arr)
{
fileContent = fileContent.Replace(str, replaceString);
}
return fileContent;
}
/// <summary>
/// 代码行数
/// </summary>
public abstract int GetCodeLineCount();
/// <summary>
/// 总行数
/// </summary>
public abstract int GetAllLineCount();
/// <summary>
/// 空白行数
/// </summary>
public abstract int GetBlankLineCount();
/// <summary>
/// 注释行数
/// </summary>
public abstract int GetCommentLineCount();
}
}
</strong>
WorldCountFactory.cs
///这个函数用来调用要统计的各种文件类型
<strong>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorldCount
{
//定义统计文件类型的总类
class WorldCountFactory
{
//声明创建统计函数
public static Count CreateCount(String type)
{
//java类文件时,返回到javaCount()函数
if (type == "java")
{
return new JavaCount();
}
//cs类文件时,返回到cshareCount()函数
if (type == "cs")
{
return new CShareCount();
}
//无文件时,返回到BlankCount()函数
return new BlankCount();
}
}
}</strong>
FileHelper.cs
///该程序用来罗列文件,获取文件内容和路径,以及新建,保存和删除文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections.Generi;
namespace WorldCount
{
//定义文件助手类
public class FileHelper
{
/// <summary>
/// 子文件List
/// </summary>
public List<String> fileList { get; set; }
public FileHelper()
{
fileList = new List<String>();
}
/// <summary>
/// 获取文件内容
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static String GetFileContent(String filePath)
{
return GetFileContent(filePath, Encoding.UTF8);
}
/// <summary>
/// 获取文件内容
/// </summary>
/// <param name="filePath"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static String GetFileContent(String filePath, Encoding encoding)
{
String fileContent = "";
using (StreamReader read = new StreamReader(filePath, encoding))
{
fileContent = read.ReadToEnd();
read.Close();
}
return fileContent;
}
/// <summary>
/// 获取文件类型
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static String GetType(String filePath)
{
String[] fileArray = filePath.Split('.');
String type = fileArray[fileArray.Length - 1];
return type;
}
/// <summary>
/// 文件保存
/// </summary>
/// <param name="fileContent"></param>
/// <param name="filePath"></param>
/// <param name="fm"></param>
public static void SaveFile(String fileContent, String filePath, FileMode fm)
{
//如果文件txt存在就打开,不存在就新建 .append 是追加写
FileStream fst = new FileStream(filePath, fm);
//写数据到txt
StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
//写入
swt.WriteLine(fileContent);
swt.Close();
fst.Close();
}
/// <summary>
/// 文件保存
/// </summary>
/// <param name="fileContent"></param>
/// <param name="filePath"></param>
public static void SaveFile(String fileContent, String filePath)
{
SaveFile(fileContent, filePath, FileMode.Create);
}
/// <summary>
/// 遍历全部文件夹
/// </summary>
/// <param name="parentFolder"></param>
public static void GetChilds(String parentFolder)
{
Console.WriteLine(parentFolder);
DirectoryInfo folder = new DirectoryInfo(parentFolder);
DirectoryInfo[] folderChild = folder.GetDirectories();
foreach (DirectoryInfo childFolder in folderChild)
{
GetChilds(childFolder.FullName);
}
}
/// <summary>
/// 获取全部子文件夹下的文件
/// </summary>
/// <param name="parentFolder"></param>
public void GetChildsFile(String parentFolder)
{
DirectoryInfo folder = new DirectoryInfo(parentFolder);
DirectoryInfo[] folderChild = folder.GetDirectories();
FileInfo[] files = folder.GetFiles();
foreach (FileInfo file in files)
{
fileList.Add(file.FullName);
}
foreach (DirectoryInfo childFolder in folderChild)
{
GetChildsFile(childFolder.FullName);
}
}
}
}
BlankCount.cs
///这个函数用来统计空白文件里面的空白行数,注释行数,编码行数,总行数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorldCount
{
//定义统计空白行新类
class BlankCount : Count
{
protected override string FilterContent(string fileContent)
{
return "";
}
//声明获取编码行的总行数
public override int GetCodeLineCount()
{
return 0;
}
//声明获取总行数
public override int GetAllLineCount()
{
return 0;
}
//声明获取空白行的总行数
public override int GetBlankLineCount()
{
return 0;
}
//声明获取注释行的总行数
public override int GetCommentLineCount()
{
return 0;
}
}
}
JavaCount.cs
///这个函数用来统计java类型的文件里面的空白行数,注释行数,编码行数,总行数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace WorldCount
{
//定义统计java文件的类
class JavaCount : Count
{
//java文件里字符串的匹配
protected override string FilterContent(string fileContent)
{
//匹配/* */
MatchCollection matchs2 = Regex.Matches(fileContent, @"/\*[\s\S]+?\*/");
foreach (Match match in matchs2)
{
String value = match.Value.ToString();
fileContent = Replace(fileContent, value, COMMMENT_REPLACE_STRING);
}
//匹配""
MatchCollection match3 = Regex.Matches(fileContent, "\"(.*?)\"");
foreach (Match match in match3)
{
String value = match.Value.ToString();
fileContent = Replace(fileContent, value, REPLACE_STRING);
}
fileContent = fileContent + '\n';
return fileContent;
}
//统计编码行的总行数
public override int GetCodeLineCount()
{
MatchCollection matchs = Regex.Matches(fileContent, @"(.*?)[\\S](.*?)\n");
return matchs.Count;
}
//统计文件的总行数
public override int GetAllLineCount()
{
MatchCollection matchs = Regex.Matches(fileContent, @"\n");
return matchs.Count;
}
//统计空白行的总行数
public override int GetBlankLineCount()
{
return GetAllLineCount() - GetCodeLineCount() - GetCommentLineCount();
}
//统计注释行的总行数
public override int GetCommentLineCount()
{
MatchCollection matchs = Regex.Matches(fileContent, @"(.*?)//(.*?)\n");
return matchs.Count;
}
}
}
CShareCount.cs
///这个函数用来统计cs类型的文件里面的空白行数,注释行数,编码行数,总行数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace WorldCount
{
//定义统计CS文件的类
class CShareCount : Count
{
//匹配cs文件中各种符号
protected override String FilterContent(string fileContent)
{
//匹配@""
MatchCollection matchs1 = Regex.Matches(fileContent, "@\"[\\s\\S]+?\"");
foreach (Match match in matchs1)
{
String value = match.Value.ToString();
fileContent = Replace(fileContent, value, REPLACE_STRING);
}
//匹配/* */
MatchCollection matchs2 = Regex.Matches(fileContent, @"/\*[\s\S]+?\*/");
foreach (Match match in matchs2)
{
String value = match.Value.ToString();
fileContent = Replace(fileContent, value, COMMMENT_REPLACE_STRING);
}
//匹配""
MatchCollection match3 = Regex.Matches(fileContent, "\"(.*?)\"");
foreach (Match match in match3)
{
String value = match.Value.ToString();
fileContent = Replace(fileContent, value, REPLACE_STRING);
}
fileContent = fileContent + '\n';
return fileContent;
}
//声明统计编码行总行数
public override int GetCodeLineCount()
{
MatchCollection matchs = Regex.Matches(fileContent, @"(.*?)[\\S](.*?)\n");
return matchs.Count;
}
//声明统计总行数
public override int GetAllLineCount()
{
MatchCollection matchs = Regex.Matches(fileContent, @"\n");
return matchs.Count;
}
//声明统计空白行总行数
public override int GetBlankLineCount()
{
return GetAllLineCount() - GetCodeLineCount() - GetCommentLineCount();
}
//声明统计注释行总行数
public override int GetCommentLineCount()
{
MatchCollection matchs = Regex.Matches(fileContent, @"(.*?)//(.*?)\n");
return matchs.Count;
}
}
}