我怎样才能读取目录c#的所有文件?文件、目录

由网友(阻击少女心)分享简介:这就是我想要做的:选择一个目录输入字符串读入一个字符串从该目录中的所有文件。这是我想实现的想法是这样的:选择的目录,然后输入一个字符串。进入该文件夹中的每个文件。例如,文件夹为:目录= {FILE1.TXT,FILE2.TXT,file3.txt} 我想先找FILE1.TXT,请先阅读所有文字,转换成字符串,看看...

这就是我想要做的:

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

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

文件或目录损坏且无法读取 怎么办

选择的目录,然后输入一个字符串。进入该文件夹中的每个文件。例如,文件夹为:目录= {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:

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}

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

阅读全文

相关推荐

最新文章