public void ListFiles(FileSystemInfo info)
{
if (!info.Exists) return;
DirectoryInfo dir = info as DirectoryInfo;
//不是目录
if (dir == null) return;
FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
//是文件
if (file != null)
{
//Console.WriteLine(file.FullName + "\t " + file.Length);
if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".jpg")
//此处为显示JPG格式,不加IF可遍历所有格式的文件
{
this.list1.Items.Add(file);
//MessageBox.Show(file.FullName.Substring(file.FullName.LastIndexOf(".")));
}
}
//对于子目录,进行递归调用
else
{
ListFiles(files[i]);
}
}
}
调用:
string dir;
ListFiles(new DirectoryInfo(dir));
过滤项目中含有中文的地方 除注释 以外
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;
using System.Data.SqlClient;
namespace SearchChina
{
public partial class SearchChinaStr : System.Web.UI.Page
{
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text = "";
string dir;
ListFiles(new DirectoryInfo(TextBox1.Text.Trim()));
}
private int j = 0;
public void ListFiles(FileSystemInfo info)
{
if (!info.Exists) return;
DirectoryInfo dir = info as DirectoryInfo;
//不是目录
if (dir == null) return;
FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
//是文件
if (file != null)
{
bool aspxcs = file.Name.ToString().Contains(".aspx.cs");//要查找的文件类型
bool cs = file.Name.ToString().Contains(".cs");
bool ascxcs = file.Name.ToString().Contains(".ascx.cs");
bool designercs = file.Name.ToString().Contains(".designer.cs");
bool aspx = file.Name.ToString().Contains(".aspx");
bool ascx = file.Name.ToString().Contains(".ascx");
bool resx = file.Name.ToString().Contains(".resx");
if ((aspxcs || cs || ascxcs) && (!designercs))
{
string strLine = "";
try
{
StreamReader sr = new StreamReader(file.FullName, Encoding.GetEncoding("GB2312"));
strLine = sr.ReadLine();
string tempstr = "";
while (strLine != null)
{
if (ChZNfilter(strLine))
{
if (IsHasChZN(strLine))
{
j++;
TextBox2.Text += "第" + Convert.ToString(j) + "行 文件路径 " + file.FullName+ " " + Convert.ToString(i) + "行 " + "\r\n" + strLine + "\r\n\r\n";
// string SqlText = null;
// SqlText = @"Insert into SearchFileTable (lineNum,filePath,fileName,searchChinaRemark,state) VALUES
// ('" + "第" + Convert.ToString(i) + "行" + "','" + file.FullName + "','" + file.Name.ToString() + "','" + strLine + "','0')";
// if (PublicClass.ExecSQL(SqlText))
// {
// TextBox2.Text = "添加成功!";
// }
// else
// {
// TextBox2.Text = "添加失败!";
// }
}
}
strLine = sr.ReadLine();
}
sr.Dispose();
sr.Close();
}
catch
{
}
}
}
//对于子目录,进行递归调用
else
{
ListFiles(files[i]);
}
}
}
/// <summary>
/// 要过滤的 标识汉字
/// </summary>
/// <param name="strLine"></param>
/// <returns>true 不过滤,false 要过滤</returns>
private bool ChZNfilter(string strLine)
{
bool rvalue = true;
if (strLine.Contains("//"))
{
rvalue = false;
}
if (strLine.Contains("///"))
{
rvalue = false;
}
if (strLine.Contains("#region"))
{
rvalue = false;
}
if (strLine.Contains("#endregion"))
{
rvalue = false;
}
if (strLine.Contains("meta:resourcekey"))
{
rvalue = false;
}
if (strLine.Contains("<title>"))
{
rvalue = false;
}
if (strLine.Contains("BizProjectLog.InsertProjectLog"))
{
rvalue = false;
}
if (strLine.Contains("this.RecordLog"))
{
rvalue = false;
}
if (strLine.Contains(".WriteLine("))
{
rvalue = false;
}
if (strLine.Contains("Exception("))
{
rvalue = false;
}
if (strLine.Contains("/*"))
{
rvalue = false;
}
if (strLine.Contains("<%--"))
{
rvalue = false;
}
if (strLine.Contains("<!--"))
{
rvalue = false;
}
return rvalue;
}
public static bool IsHasChZN(string InputText)
{
return RegCHZN.IsMatch(InputText);
}
}
}