我怎样才能读取目录c#的所有文件? [英] How can I read all files from directory c#?

查看:196
本文介绍了我怎样才能读取目录c#的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:

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

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

选择的目录,然后输入一个字符串。进入该文件夹中的每个文件。例如,文件夹为:目录= {FILE1.TXT,FILE2.TXT,file3.txt}

我想先找FILE1.TXT,请先阅读所有文字,转换成字符串,看看我的字符串是在该文件中。如果是的话:做别的去FILE2.TXT,等等

解决方案

 的foreach(字符串Directory.GetFiles文件名(目录名,是searchPattern)
{
    字符串[] fileLines = File.ReadAllLines(文件名);
    //做一些与该文件内容
}
 

您可以使用File.RealAllBytes()'或'File.ReadAllText()而不是Fi​​le.ReadAllLines(),以及 - 取决于你的需求。

This is what I want to do:

  1. Select a directory
  2. Input a string
  3. Read all files from that directory in a string.

The idea that I wanna implement is this:

Select the directory, and input a string. Go to each file from that folder. For example the folder is: Directory={file1.txt,file2.txt,file3.txt}

I wanna go to file1.txt first, read all the text, into a string, and see if my string is in that file. If yes: do else go to file2.txt, and so on.

解决方案

foreach (string fileName in Directory.GetFiles("directoryName", "searchPattern")
{
    string[] fileLines = File.ReadAllLines(fileName);
    // Do something with the file content
}

You can use 'File.RealAllBytes()' or 'File.ReadAllText()' instead of 'File.ReadAllLines()' as well - depends on your requirement

这篇关于我怎样才能读取目录c#的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆