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


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

  • 相关阅读:
    hdu 2492 树状数组 Ping pong
    HDU 1532 基础EK Drainage Ditches
    EK算法模板
    Codeforces Round #538 (Div. 2) (A-E题解)
    Codeforces Global Round 1 (A-E题解)
    Educational Codeforces Round 59 (Rated for Div. 2) DE题解
    Codeforces Round #535 (Div. 3) 题解
    Codeforces Round #534 (Div. 2) D. Game with modulo(取余性质+二分)
    POJ2253:Frogger(改造Dijkstra)
    POJ1797:Heavy Transportation(改造Dijkstra)
  • 原文地址:https://www.cnblogs.com/liufei88866/p/2186326.html
Copyright © 2020-2023  润新知