C#遍历整个文件夹及子目录的文件代码,.Net技术文章,Asp.net系列教程,Asp.n...

本文介绍了如何使用C#语言编写代码,实现遍历指定目录下所有文件,并筛选出特定类型的文件。同时,通过正则表达式过滤掉包含中文字符的代码行,以确保只保留纯英文内容。

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);
        }
    }
}

 

转载于:https://www.cnblogs.com/liufei88866/archive/2011/09/23/2186326.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值