【问题标题】:How can I read all files from directory c#?如何从目录 c# 中读取所有文件?
【发布时间】:2012-06-10 18:39:06
【问题描述】:

这就是我想做的:

  1. 选择目录
  2. 输入一个字符串
  3. 以字符串形式读取该目录中的所有文件。

我想实现的想法是这样的:

选择目录,输入字符串。转到该文件夹​​中的每个文件。例如文件夹为:Directory={file1.txt,file2.txt,file3.txt}

我想先去file1.txt,把所有的文本读入一个字符串,看看我的字符串是否在那个文件中。如果是:是否转到 file2.txt,等等。

【问题讨论】:

  • 有些 SO 用户会马上给你你需要的代码,但是为了大家,请阅读这个网站:whathaveyoutried.com SO 不会总是给你你想要的直接答案,你会也必须做一些工作。

标签: c# .net string file streamreader


【解决方案1】:
foreach (string fileName in Directory.GetFiles("directoryName", "searchPattern")
{
    string[] fileLines = File.ReadAllLines(fileName);
    // Do something with the file content
}

您也可以使用File.ReadAllBytes()File.ReadAllText() 代替File.ReadAllLines(),这取决于您的要求。

【讨论】:

  • 我收到此错误... 错误 1 ​​当前上下文中不存在名称“文件”我应该添加一些内容吗?
  • 是的,添加“使用 System.IO;”在文件顶部的其他 using 语句旁边
【解决方案2】:
        var searchTerm = "SEARCH_TERM";
        var searchDirectory = new System.IO.DirectoryInfo(@"c:\Test\");

        var queryMatchingFiles =
                from file in searchDirectory.GetFiles()
                where file.Extension == ".txt"
                let fileContent = System.IO.File.ReadAllText(file.FullName)
                where fileContent.Contains(searchTerm)
                select file.FullName;

        foreach (var fileName in queryMatchingFiles)
        {
            // Do something
            Console.WriteLine(fileName);
        }

这是一个基于 LINQ 的解决方案,应该也可以解决您的问题。它可能更容易理解和维护。因此,如果您能够使用 LINQ,请尝试一下。

【讨论】:

    【解决方案3】:

    我想这就是你想要的......

    string input = "blah blah";
    string file_content;
    FolderBrowserDialog fld = new FolderBrowserDialog();
    if (fld.ShowDialog() == DialogResult.OK)
    {
        DirectoryInfo di = new DirectoryInfo(fld.SelectedPath);
        foreach(string f  in Directory.GetFiles(fld.SelectedPath))
        {
            file_content = File.ReadAllText(f);
            if (file_content.Contains(input))
            {
                //string found
                break;
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      您好,实现您所要求的最简单的方法是这样的:

      string[] Files = System.IO.Directory.GetFiles("Directory_To_Look_In");
      
      foreach (string sFile in Files)
      {
          string fileCont = System.IO.File.ReadAllText(sFile);
          if (fileCont.Contains("WordToLookFor") == true)
          {
              //it found something
          }
      
      }
      

      【讨论】:

      • 谢谢,我收到此错误... 错误 1 ​​当前上下文中不存在名称“文件”
      • 很好,我还稍微编辑了代码并进行了测试编译。 :)
      【解决方案5】:
                  // Only get files that are text files only as you want only .txt 
                  string[] dirs = Directory.GetFiles("target_directory", "*.txt");
                  string fileContent = string.Empty;
                  foreach (string file in dirs) 
                  {
                     // Open the file to read from. 
                      fileContent = File.ReadAllText(file);                
                      // alternative: Use StreamReader to consume the entire text file.
                      //StreamReader reader = new StreamReader(file);
                      //string fileContent = reader.ReadToEnd();
      
                      if(fileContent.Contains("searching_word")){
                        //do whatever you want
                        //exit from foreach loop as you find your match, so no need to iterate
                          break;
                      }
      
                  }
      

      【讨论】:

        猜你喜欢
        • 2021-11-06
        • 1970-01-01
        • 2018-07-04
        • 2015-07-03
        • 1970-01-01
        • 2021-09-20
        • 2021-12-15
        • 2013-06-20
        • 1970-01-01
        相关资源
        最近更新 更多