如何从目录 c# 中读取所有文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10971321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I read all files from directory c#?
提问by icebox19
This is what I want to do:
这就是我想要做的:
- Select a directory
- Input a string
- 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}
选择目录,并输入一个字符串。转到该文件夹中的每个文件。例如文件夹是: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.
我想先转到 file1.txt,将所有文本读入一个字符串,然后查看我的字符串是否在该文件中。如果是:否则转到 file2.txt,依此类推。
回答by eyossi
foreach (string fileName in Directory.GetFiles("directoryName", "searchPattern")
{
string[] fileLines = File.ReadAllLines(fileName);
// Do something with the file content
}
You can use File.ReadAllBytes()or File.ReadAllText()instead of File.ReadAllLines()as well, it just depends on your requirements.
您也可以使用File.ReadAllBytes()或File.ReadAllText()代替File.ReadAllLines(),这仅取决于您的要求。
回答by Md Kamruzzaman Sarker
I think this is what you want...
我想这就是你想要的......
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;
}
}
}
回答by JaggenSWE
Hi the easiest way to achieve what you're asking for would be something like this:
嗨,实现您所要求的最简单方法是这样的:
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
}
}
回答by Jason De Oliveira
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);
}
This is a solution based on LINQ, which should also solve your problem. It might be easier to understand and easier to maintain. So if you are able to use LINQ give it a try.
这是一个基于 LINQ 的解决方案,它也应该可以解决您的问题。它可能更容易理解和更容易维护。因此,如果您能够使用 LINQ,请尝试一下。
回答by Humayoun_Kabir
// 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;
}
}

