class FileTotal
{
private static FileStream fs;
private static StreamWriter sw;
static void Main(string[] args)
{
string path = @"D:\Total";
if (Directory.Exists(path) == false)
{
Console.WriteLine(path);
Console.ReadKey();
}
else
{
fs = new FileStream(@"D:\result.txt", FileMode.Create);
sw = new StreamWriter(fs);
sw.WriteLine("Result:");
sw.WriteLine(path);
listDirectory(path);
sw.Flush();
sw.Close();
fs.Close();
Console.WriteLine("Continue!");
Console.ReadKey();
}
}
private static void listDirectory(string path)
{
DirectoryInfo folder = new DirectoryInfo(path);
//遍历文件
foreach (FileInfo nextFile in folder.GetFiles())
{
if (nextFile.Name.Contains(".vbproj") == true ||
nextFile.Name.Contains("AssemblyInfo.vb") == true)
{
continue;
}
if (nextFile.Name.Contains(".vb") == true ||
nextFile.Name.Contains(".xml") == true ||
nextFile.Name.Contains(".resx") == true ||
nextFile.Name.Contains(".sql") == true ||
nextFile.Name.Contains(".SQL") == true)
{
sw.Write('\t');
sw.Write("---->");
if (nextFile.Name.Contains(".vb") == true)
{
GetVBFileTotal(nextFile);
}
if (nextFile.Name.Contains(".sql") == true ||
nextFile.Name.Contains(".SQL") == true)
{
GetSqlFileTotal(nextFile);
}
if (nextFile.Name.Contains(".xml") == true ||
nextFile.Name.Contains(".resx") == true)
{
GetXmlFileTotal(nextFile);
}
}
}
foreach (DirectoryInfo nextFolder in folder.GetDirectories())
{
if (nextFolder.Name == "bin" || nextFolder.Name == "obj")
{
continue;
}
sw.Write('\t');
sw.Write("--)");
sw.WriteLine(nextFolder.FullName);
listDirectory(nextFolder.FullName);
}
}
private static void GetXmlFileTotal(FileInfo nextFile)
{
int jiCount = 0;
int bankCount = 0;
int commentCount = 0;
using (StreamReader sr = new StreamReader(nextFile.FullName))
{
string[] txtlist = sr.ReadToEnd().Split('\n');
string preStr = string.Empty;
foreach (string str in txtlist)
{
if (!string.IsNullOrEmpty(str))
{
if (str.Trim().Length != 0)
{
if (str.Trim().IndexOf("") == true)
{
jiCount++;
}
}
else
{
bankCount++;
}
}
else
{
bankCount++;
}
if (str.Contains("-->") == true)
{
preStr = str;
}
}
sw.WriteLine(nextFile.FullName + "\t" + txtlist.Length + "\t" + jiCount + "\t" + commentCount + "\t" + bankCount);
}
}
private static void GetSqlFileTotal(FileInfo nextFile)
{
int jiCount = 0;
int bankCount = 0;
int commentCount = 0;
using (StreamReader sr = new StreamReader(nextFile.FullName))
{
string[] txtlist = sr.ReadToEnd().Split('\n');
foreach (string str in txtlist)
{
if (!string.IsNullOrEmpty(str))
{
if (str.Trim().Length != 0)
{
if (str.Trim().IndexOf("/*") == 0 || str.Trim().IndexOf("--") == 0)
{
commentCount++;
}
else
{
jiCount++;
}
}
else
{
bankCount++;
}
}
else
{
bankCount++;
}
}
sw.WriteLine(nextFile.FullName + "\t" + txtlist.Length + "\t" + jiCount + "\t" + commentCount + "\t" + bankCount);
}
}
private static void GetVBFileTotal(FileInfo nextFile)
{
int jiCount = 0;
int bankCount = 0;
int commentCount = 0;
using (StreamReader sr = new StreamReader(nextFile.FullName))
{
string[] txtlist = sr.ReadToEnd().Split('\n');
foreach (string str in txtlist)
{
if (!string.IsNullOrEmpty(str))
{
if (str.Trim().Length != 0)
{
if (str.Trim().IndexOf("'") == 0 || str.Trim().IndexOf("#Region") == 0 || str.Trim().IndexOf("#End Region") == 0)
{
commentCount++;
}
else
{
jiCount++;
}
}
else
{
bankCount++;
}
}
else
{
bankCount++;
}
}
sw.WriteLine(nextFile.FullName + "\t" + txtlist.Length + "\t" + jiCount + "\t" + commentCount + "\t" + bankCount);
}
}
}