使用txt文件的递归函数搜索目录

时间:2014-04-02 18:17:43

标签: c# visual-studio-2010 visual-c#-express-2010

我有一个具有所选文件夹的功能,可查看所有扩展名为.txt的文件。

问题是它只会搜索并浏览所选文件夹中的文件夹。它不会查看所选文件夹顶层中的.txt文件,只查看子目录中的文件:

Chosen_Folder


test1.txt的  的test2.txt  文件夹1  FOLDER2

所以换句话说,它忽略了test1.txt,test2.txt,但它会找到文件夹中的所有.txt文件以及那些文件夹子目录。

这是我的代码:

public static void DirSearch(string sDir)
    {




        string ExtractedTXTBlocks = FINAL_PATH + "\\" + "Extracted TXT Blocks";
        System.IO.Directory.CreateDirectory(ExtractedTXTBlocks);

        try
        {
            foreach (string d in Directory.GetDirectories(sDir))
            {
                foreach (string f in Directory.GetFiles(d))
                {
                    string[] filelines = File.ReadAllLines(f);
                    string line = "";
                    string sBuilder = "";
                    int i = 1;
                    string temp = "";

                    string Fname = f.Substring(f.LastIndexOf("\\"), (f.Length - f.LastIndexOf("\\"))).Replace("\\","");
                    string Dname = d.Substring(d.LastIndexOf("\\"), (d.Length - d.LastIndexOf("\\"))).Replace("\\","");
                    StreamWriter sW = new StreamWriter(System.IO.Path.Combine(ExtractedTXTBlocks, Dname +"-" +Fname), true);


                    foreach (string item in filelines)
                    {
                        i++;
                        line = item;


                        line = (line.Replace("CMD", "")).Trim();
                        line = (line.Replace("0x", "")).Trim();
                        line = (line.Replace("0X", "")).Trim();
                        line = (line.Replace("DI", "")).Trim();
                        line = (line.Replace("SW", "")).Trim();
                        line = (line.Replace("LO 0", "")).Trim();
                        line = (line.Replace("LI", "")).Trim();
                        line = (line.Replace("LE 0", "")).Trim();
                        line = (line.Replace("REM", "")).Trim();
                        line = (line.Replace("$", "")).Trim();
                        line = (line.Replace("LO", "")).Trim();
                        line = (line.Replace("DO", "")).Trim();
                        line = (line.Replace(" ", "")).Trim();

                        if (line.StartsWith("1234") || line.StartsWith("1233"))
                        {

                            temp = line.Substring(8, line.Length - 8);
                            int number = int.Parse(temp);
                            temp = line.Substring(8, line.Length - 8).Replace(line.Substring(8, line.Length - 8), number.ToString("x").ToUpper());
                            line = line.Remove(8, line.Length - 8);
                            line += temp;
                            sBuilder = line;


                        }
                        else if (line.StartsWith("1235") || line.StartsWith("1255"))
                        {

                            temp = line.Substring(8, line.Length - 8);
                            int number = int.Parse(temp);                                
                            temp = line.Substring(8, line.Length - 8).Replace(line.Substring(8, line.Length - 8),number.ToString("x").ToUpper());
                            line = line.Remove(8, line.Length - 8);
                            line += temp;
                            sBuilder = line;


                        }

                        else if (line.Equals("Success"))
                        {

                            sW.WriteLine(sBuilder);                                
                            sBuilder = "";

                        }                            
                        else
                        {
                            if (!line.Equals(""))
                            {
                                sBuilder += line;
                            }


                        }





                    }
                    sW.Close();



                }
                DirSearch(d);
            }
        }
        catch (System.Exception excpt)
        {
            Console.WriteLine(excpt.Message);
        }
    }

我尝试在get文件夹方法Directory.GetDirectories中将SearchOption设置为AllDirectories,但我认为这不是我需要做的。我担心我会错过一些非常小的东西。

亲切的问候。

2 个答案:

答案 0 :(得分:1)

试试这个:

 Directory.GetFiles(d,"*.txt")

答案 1 :(得分:0)

感谢您的帮助,但我在这里找到了另一个选择:

public static void DirSearch(string sDir)
    {




        string ExtractedTXTBlocks = FINAL_PATH + "\\" + "Extracted TXT Blocks";
        System.IO.Directory.CreateDirectory(ExtractedTXTBlocks);

        try
        {

                foreach (string f in Directory.GetFiles(sDir, "*.TXT", SearchOption.AllDirectories))
                {
                    string[] filelines = File.ReadAllLines(f);
                    string line = "";
                    string sBuilder = "";
                    int i = 1;
                    string temp = "";

                    string Fname = f.Substring(f.LastIndexOf("\\"), (f.Length - f.LastIndexOf("\\"))).Replace("\\","");
                    string Dname = Path.GetDirectoryName(f);
                    Dname = Dname.Substring(Dname.LastIndexOf("\\"), Dname.Length - Dname.LastIndexOf("\\")).Replace("\\", "");

                    StreamWriter sW = new StreamWriter(ExtractedTXTBlocks + "\\" + Dname + "-" + Fname, true);

                    foreach (string item in filelines)
                    {
                        i++;
                        line = item;


                        line = (line.Replace("CMD", "")).Trim();
                        line = (line.Replace("0x", "")).Trim();
                        line = (line.Replace("0X", "")).Trim();
                        line = (line.Replace("DI", "")).Trim();
                        line = (line.Replace("SW", "")).Trim();
                        line = (line.Replace("LO 0", "")).Trim();
                        line = (line.Replace("LI", "")).Trim();
                        line = (line.Replace("LE 0", "")).Trim();
                        line = (line.Replace("REM", "")).Trim();
                        line = (line.Replace("$", "")).Trim();
                        line = (line.Replace("LO", "")).Trim();
                        line = (line.Replace("DO", "")).Trim();
                        line = (line.Replace(" ", "")).Trim();

                        if (line.StartsWith("1234") || line.StartsWith("1233"))
                        {

                            temp = line.Substring(8, line.Length - 8);
                            int number = int.Parse(temp);
                            temp = line.Substring(8, line.Length - 8).Replace(line.Substring(8, line.Length - 8), number.ToString("x").ToUpper());
                            line = line.Remove(8, line.Length - 8);
                            line += temp;
                            sBuilder = line;


                        }
                        else if (line.StartsWith("1235") || line.StartsWith("1255"))
                        {

                            temp = line.Substring(8, line.Length - 8);
                            int number = int.Parse(temp);                                
                            temp = line.Substring(8, line.Length - 8).Replace(line.Substring(8, line.Length - 8),number.ToString("x").ToUpper());
                            line = line.Remove(8, line.Length - 8);
                            line += temp;
                            sBuilder = line;


                        }

                        else if (line.Equals("Success"))
                        {

                            sW.WriteLine(sBuilder);                                
                            sBuilder = "";

                        }                            
                        else
                        {
                            if (!line.Equals(""))
                            {
                                sBuilder += line;
                            }


                        }





                    }
                    sW.Close();



                }

        }
        catch (System.Exception excpt)
        {
            Console.WriteLine(excpt.Message);
        }
    }

这将只浏览我要查看的文件夹中的所有文件,包括子目录。我想使用递归方案,但它并没有为我想做的事情做好充分优化。

亲切的问候。